最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

MacOS, AppKit - padding of window title in toolbar - Stack Overflow

programmeradmin0浏览0评论

In MacOS app, I have a window created in storyboard, with toolbar style Unified. When launched, the window looks like this:

I need the window title ("General") to have more left padding, in order to be aligned with "Actions" in the window content. How to achieve that?

In MacOS app, I have a window created in storyboard, with toolbar style Unified. When launched, the window looks like this:

I need the window title ("General") to have more left padding, in order to be aligned with "Actions" in the window content. How to achieve that?

Share Improve this question asked yesterday KavenKaven 3392 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

In the end, I have created a custom NSToolbarItem, that is just text styled the same way as usual window title, but is positioned with required padding

class ToolbarLabelView : NSView {  
    var label: NSTextField!  
      
    public override init(frame frameRect: NSRect) {  
        super.init(frame: frameRect)  
  
        self.label = NSTextField(labelWithString: "MyLabel")  
        self.label.translatesAutoresizingMaskIntoConstraints = false  
        self.addSubview(self.label)  
          
        self.label.font = NSFont.systemFont(ofSize: 15, weight: .semibold)  
          
        NSLayoutConstraint.activate(\[  
            self.widthAnchor.constraint(equalToConstant: 200),  
  
            self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 105),  
            self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor)  
        \])  
    }  
      
    required init?(coder: NSCoder) {  
        super.init(coder: coder)  
    }  
      
    public func SetLabelText(\_ str: String) {  
        self.label.stringValue = str  
    }  
}  
  
class LabelToolbarItem: NSToolbarItem {  
     
    override init(itemIdentifier: NSToolbarItem.Identifier) {  
        super.init(itemIdentifier: itemIdentifier)  
          
        self.view = ToolbarLabelView()  
        self.view?.translatesAutoresizingMaskIntoConstraints = false  
    }  
}

If then I hide standard window title like this window?.titleVisibility = .hidden, only the custom toolbar item is there and it looks like window title. It looks as it should, even though it is not very clean solution.

发布评论

评论列表(0)

  1. 暂无评论