NSWindow:更改窗口按钮的位置

时间:2015-05-06 19:13:46

标签: swift macos cocoa appkit

我想伪造一个标题栏(更大,颜色不同),所以到目前为止我的方法如下:

我在标题栏的正下方添加了一个NSView,然后使用以下代码将标题栏设置为透明:

self.window.titlebarAppearsTransparent = true
self.window.styleMask |= NSFullSizeContentViewWindowMask    

下一步是,我将NSView子类化为添加一些绘图方法(背景等),特别是代码,这样我就可以使用完整的NSView来移动窗口(因此我使用这段代码:{{3 }})

结果如下:

WindowScreen

现在我要做的下一件事就是在这个新标题栏中垂直居中交通灯按钮。我知道,我可以使用self.window.standardWindowButton(NSWindowButton.CloseButton)(例如)访问按钮。但是更改其中一个按钮的frame.origin没有任何效果。

如何更改按钮的origin.y值?

更新

我发现,窗口调整大小会重新排列按钮。现在我决定将按钮作为子视图添加到我的假标题栏中,因为在标题栏中移动原点会切断按钮(显然它仅限于标题栏)。

这很有效,但奇怪的是按钮的鼠标悬停效果仍然保留在标题栏中。看看这个屏幕:

Second screen

这实际上是我的代码:

func moveButtons() {
    self.moveButtonDownFirst(self.window.standardWindowButton(NSWindowButton.CloseButton)!)
    self.moveButtonDownFirst(self.window.standardWindowButton(NSWindowButton.MiniaturizeButton)!)
    self.moveButtonDownFirst(self.window.standardWindowButton(NSWindowButton.ZoomButton)!)
}

func moveButtonDownFirst(button: NSView) {
    button.setFrameOrigin(NSMakePoint(button.frame.origin.x, button.frame.origin.y+10.0))
    self.fakeTitleBar.addSubview(button)
}

3 个答案:

答案 0 :(得分:1)

您需要添加工具栏并更改窗口属性titleVisibility。这里有更多详细信息NSWindow Style Showcase

let customToolbar = NSToolbar()
window?.titleVisibility = .hidden
window?.toolbar = customToolbar

enter image description here

答案 1 :(得分:0)

Swift 4.2版本(无工具栏)。

背后的想法:

  • 我们调整了标准窗口按钮的框架,没有更改超级视图。
  • 为防止剪切,我们需要增加标题栏的高度。这可以通过添加透明的标题栏附件来实现。
  • 当窗口进入全屏模式时,我们隐藏标题栏附件。
  • 当窗口超出全屏显示时,我们显示标题栏附件。
  • 此外,我们需要调整全屏模式下标准按钮旁边显示的UI元素的布局。

普通屏幕。

enter image description here

全屏模式。

enter image description here

真实应用

enter image description here


文件 FullContentWindow.swift

Expression<Func

文件 FullContentWindowController.swift

public class FullContentWindow: Window {

   private var buttons: [NSButton] = []

   public let titleBarAccessoryViewController = TitlebarAccessoryViewController()
   private lazy var titleBarHeight = calculatedTitleBarHeight
   private let titleBarLeadingOffset: CGFloat?
   private var originalLeadingOffsets: [CGFloat] = []

   public init(contentRect: NSRect, titleBarHeight: CGFloat, titleBarLeadingOffset: CGFloat? = nil) {
      self.titleBarLeadingOffset = titleBarLeadingOffset
      let styleMask: NSWindow.StyleMask = [.closable, .titled, .miniaturizable, .resizable, .fullSizeContentView]
      super.init(contentRect: contentRect, styleMask: styleMask, backing: .buffered, defer: true)
      titleVisibility = .hidden
      titlebarAppearsTransparent = true
      buttons = [NSWindow.ButtonType.closeButton, .miniaturizeButton, .zoomButton].compactMap {
         standardWindowButton($0)
      }
      var accessoryViewHeight = titleBarHeight - calculatedTitleBarHeight
      accessoryViewHeight = max(0, accessoryViewHeight)
      titleBarAccessoryViewController.view.frame = CGRect(dimension: accessoryViewHeight) // Width not used.
      if accessoryViewHeight > 0 {
         addTitlebarAccessoryViewController(titleBarAccessoryViewController)
      }
      self.titleBarHeight = max(titleBarHeight, calculatedTitleBarHeight)
   }

   public override func layoutIfNeeded() {
      super.layoutIfNeeded()
      if originalLeadingOffsets.isEmpty {
         let firstButtonOffset = buttons.first?.frame.origin.x ?? 0
         originalLeadingOffsets = buttons.map { $0.frame.origin.x - firstButtonOffset }
      }
      if titleBarAccessoryViewController.view.frame.height > 0, !titleBarAccessoryViewController.isHidden {
         setupButtons()
      }
   }

}

extension FullContentWindow {

   public var standardWindowButtonsRect: CGRect {
      var result = CGRect()
      if let firstButton = buttons.first, let lastButton = buttons.last {
         let leadingOffset = firstButton.frame.origin.x
         let maxX = lastButton.frame.maxX
         result = CGRect(x: leadingOffset, y: 0, width: maxX - leadingOffset, height: titleBarHeight)
         if let titleBarLeadingOffset = titleBarLeadingOffset {
            result = result.offsetBy(dx: titleBarLeadingOffset - leadingOffset, dy: 0)
         }
      }
      return result
   }

}

extension FullContentWindow {

   private func setupButtons() {
      let barHeight = calculatedTitleBarHeight
      for (idx, button) in buttons.enumerated() {
         let coordY = (barHeight - button.frame.size.height) * 0.5
         var coordX = button.frame.origin.x
         if let titleBarLeadingOffset = titleBarLeadingOffset {
            coordX = titleBarLeadingOffset + originalLeadingOffsets[idx]
         }
         button.setFrameOrigin(CGPoint(x: coordX, y: coordY))
      }
   }

   private var calculatedTitleBarHeight: CGFloat {
      let result = contentRect(forFrameRect: frame).height - contentLayoutRect.height
      return result
   }
}

用法

open class FullContentWindowController: WindowController {

   private let fullContentWindow: FullContentWindow
   private let fullContentViewController = ViewController()

   public private (set) lazy var titleBarContentContainer = View().autolayoutView()
   public private (set) lazy var contentContainer = View().autolayoutView()

   private lazy var titleOffsetConstraint =
      titleBarContentContainer.leadingAnchor.constraint(equalTo: fullContentViewController.contentView.leadingAnchor)

   public init(contentRect: CGRect, titleBarHeight: CGFloat, titleBarLeadingOffset: CGFloat? = nil) {
      fullContentWindow = FullContentWindow(contentRect: contentRect, titleBarHeight: titleBarHeight,
                                            titleBarLeadingOffset: titleBarLeadingOffset)
      super.init(window: fullContentWindow, viewController: fullContentViewController)
      contentWindow.delegate = self
      fullContentViewController.contentView.addSubviews(titleBarContentContainer, contentContainer)

      let standardWindowButtonsRect = fullContentWindow.standardWindowButtonsRect

      LayoutConstraint.withFormat("V:|[*][*]|", titleBarContentContainer, contentContainer).activate()
      LayoutConstraint.pin(to: .horizontally, contentContainer).activate()
      LayoutConstraint.constrainHeight(constant: standardWindowButtonsRect.height, titleBarContentContainer).activate()
      LayoutConstraint.withFormat("[*]|", titleBarContentContainer).activate()
      titleOffsetConstraint.activate()

      titleOffsetConstraint.constant = standardWindowButtonsRect.maxX
   }

   open override func prepareForInterfaceBuilder() {
      titleBarContentContainer.backgroundColor = .green
      contentContainer.backgroundColor = .yellow
      fullContentViewController.contentView.backgroundColor = .blue
      fullContentWindow.titleBarAccessoryViewController.contentView.backgroundColor = Color.red.withAlphaComponent(0.4)
   }

   public required init?(coder: NSCoder) {
      fatalError()
   }
}

extension FullContentWindowController {

   public func embedTitleBarContent(_ viewController: NSViewController) {
      fullContentViewController.embedChildViewController(viewController, container: titleBarContentContainer)
   }

   public func embedContent(_ viewController: NSViewController) {
      fullContentViewController.embedChildViewController(viewController, container: contentContainer)
   }
}

extension FullContentWindowController: NSWindowDelegate {

   public func windowWillEnterFullScreen(_ notification: Notification) {
      fullContentWindow.titleBarAccessoryViewController.isHidden = true
      titleOffsetConstraint.constant = 0
   }

   public  func windowWillExitFullScreen(_ notification: Notification) {
      fullContentWindow.titleBarAccessoryViewController.isHidden = false
      titleOffsetConstraint.constant = fullContentWindow.standardWindowButtonsRect.maxX
   }
}

答案 2 :(得分:0)

我的答案涉及@Vlad和@Lupurus的一些答案。要更改按钮位置,只需对NSWindow子类中的函数func moveButton(ofType type: NSWindow.ButtonType)进行调用即可处理移动。

注意:就我而言,我只需要将按钮降低2像素即可。

要处理正常情况(不是全屏),我刚刚重写了NSWindow的功能func standardWindowButton(_ b: NSWindow.ButtonType) -> NSButton?,以便在返回按钮之前根据需要移动按钮。

注意:更好的代码将具有一种单独的方法来计算新框架,并将新值存储在其他地方

要从全屏返回时正确处理动画,我们需要重写NSWindow的func layoutIfNeeded()方法,从全屏返回的动画在需要时将调用此方法。

我们需要将更新的框架保存在NSWindow中。 nil值将触发帧重新计算。

您需要将更新的框架保留在NSWindow窗口中:

    var closeButtonUpdatedFrame: NSRect?
    var miniaturizeButtonUpdatedFrame: NSRect?
    var zoomButtonUpdatedFrame: NSRect?

    public override func layoutIfNeeded() {
        super.layoutIfNeeded()

        if closeButtonUpdatedFrame == nil {
            moveButton(ofType: .closeButton)
        }

        if miniaturizeButtonUpdatedFrame == nil {
            moveButton(ofType: .miniaturizeButton)
        }

        if zoomButtonUpdatedFrame == nil {
            moveButton(ofType: .zoomButton)
        }
    }

    override public func standardWindowButton(_ b: NSWindow.ButtonType) -> NSButton? {

        switch b {
        case .closeButton:
            if closeButtonUpdatedFrame == nil {
                moveButton(ofType: b)
            }
        case .miniaturizeButton:
            if miniaturizeButtonUpdatedFrame == nil {
                moveButton(ofType: b)
            }
        case .zoomButton:
            if zoomButtonUpdatedFrame == nil {
                moveButton(ofType: b)
            }
        default:
            break
        }
        return super.standardWindowButton(b)
    }

    func moveButton(ofType type: NSWindow.ButtonType) {

        guard let button = super.standardWindowButton(type) else {
            return
        }

        switch type {
        case .closeButton:
            self.moveButtonDown(button: button)
            closeButtonUpdatedFrame = button.frame
        case .miniaturizeButton:
            self.moveButtonDown(button: button)
            miniaturizeButtonUpdatedFrame = button.frame
        case .zoomButton:
            self.moveButtonDown(button: button)
            zoomButtonUpdatedFrame = button.frame
        default:
            break
        }
    }

    func moveButtonDown(button: NSView) {

        button.setFrameOrigin(NSMakePoint(button.frame.origin.x, button.frame.origin.y-2.0))
    }

要处理全屏情况,我们需要在NSWindowDelegate中放置一些代码,在我的情况下,该委托是NSWindowController实例。此代码将强制func layoutIfNeeded()方法从全屏显示时重新计算按钮帧:


    public func windowWillExitFullScreen(_ notification: Notification) {

        self.window.closeButtonUpdatedFrame = nil
        self.window.miniaturizeButtonUpdatedFrame = nil
        self.window.zoomButtonUpdatedFrame = nil
    }

等等!

在我的测试中,此代码可以处理所有情况。