Post not yet marked as solved
Dear friend,
It works in iOS, but how to do it on Cocoa(macOS Application)?
There is no UIApplication, and no open functions on NSApplication.shared.
How to open safari in Cocoa application?
Post not yet marked as solved
Thanks a lot, you did me a great favor.
Post not yet marked as solved
I've found the reason caused the problem: As I set the WindowMask in WindowController. Thanks All.
Can we ask Apple's Architects to improve it?
To reproduce the problem, Add enough items to a tabView like this:
var tabViewItem:NSTabViewItem=NSTabViewItemExtension.init()
tabViewItem.label="192.168.255.254"
var objects:NSArray?=nil
Bundle.main.loadNibNamed("OperationTab", owner: nil, topLevelObjects: &objects)
for object in objects!{
if object is NSView{
tabViewItem.view=object as! NSView
}
if object is OperationTabViewController{
tabViewItem.viewController=object as! OperationTabViewController
}
}
var vc=tabViewItem.viewController as! OperationTabViewController
vc.setServerName(serverName: "192.168.255.254")
mainTab.addTabViewItem(tabViewItem)
objects=nil
tabViewItem=NSTabViewItemExtension.init()
tabViewItem.label="192.168.255.253"
Bundle.main.loadNibNamed("OperationTab", owner: nil, topLevelObjects: &objects)
for object in objects!{
if object is NSView{
tabViewItem.view=object as! NSView
}
if object is OperationTabViewController{
tabViewItem.viewController=object as! OperationTabViewController
}
}
vc=tabViewItem.viewController as! OperationTabViewController
vc.setServerName(serverName: "192.168.255.253")
mainTab.addTabViewItem(tabViewItem)
...
(need add enough items)
Then do the function while click a 'Close' button:
mainTab.removeTabViewItem(item)
func removeTabView(operationView:NSView){
for item in mainTab.tabViewItems{
if item.view == operationView{
mainTab.removeTabViewItem(item)
break;
}
}
mainTab.setNeedsDisplay(mainTab.frame)
mainTab.display()
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
OperationQueue.main.addOperation {[weak self]in
print("displayIfNeeded()")
self?.mainTab.displayIfNeeded()
self?.mainTab.display()
self?.view.displayIfNeeded()
self?.view.display()
self!.view.resizeSubviews(withOldSize: NSSize.init(width: self!.view.frame.width, height: self!.view.frame.height))
self!.mainTab.resizeSubviews(withOldSize: NSSize.init(width: self!.mainTab.frame.width, height: self!.mainTab.frame.height))
}
}
}
No effect, while the 'Close' button clicked, it will call the function removeTabView(), and the tabViewItem was removed, but the tabView's labels not completely redrawed.
Yes, I override the drawLabel function, and did the NSString.draw(inrect) to show the three points while the width of the item’s label is not enough. But the drawLabel function always only called twice while I do the removeTabViewItem one time in more than 10 tabviewItems.
The problem is occurring here:
The func drawLabel(_ shouldTruncateLabel: Bool, in labelRect: NSRect) of NSTabViewItem only called twice if there are enough items that item labels has truncated.
Is there any solution to redraw all labels of tabViewItems?
I have tried using: mainTab.displayIfNeeded() and mainTab.display() , but not effect.