gpt4 book ai didi

cocoa - NSView 覆盖 NSPageController 在转换时消失

转载 作者:行者123 更新时间:2023-12-03 16:45:31 25 4
gpt4 key购买 nike

我有一个 NSPageController,其中包含 8 个左右的 NSViewController。当鼠标位于窗口内部时,我希望有一个半透明的底部栏,并且无论鼠标在哪里,都有一个半透明的顶部栏。

我将顶部栏和底部栏添加到 View 中,以及 NSPageControllers viewDidLoad() 方法中的约束。

它们在第一页上显示得很好,但是当我开始从一个页面转换到另一个页面时,新的 NSViewController 会在覆盖的 View 上重新绘制,然后它们就会消失。我可以验证它们是否位于 NSViewControllers 下,因为然后我一直拖动到特定的一侧,我可以在下面看到它们。

有什么想法为什么会发生这种情况/我如何避免它?

代码:

class MyPageController: NSPageController {

// MARK: - Properties
fileprivate var mouseIsInside = false

fileprivate var tabBar: TabBar!

// MARK: - NSViewController
override func viewDidLoad() {
super.viewDidLoad()

// add tab bar, then hide it (mouse in or outside of window will restore current state)
tabBar = TabBar(frame: NSRect(x: 0, y:0, width: view.frame.size.width, height: 40))
addTabBar(withAnimation: false)
removeTabBar(withAnimation: false)

NSLayoutConstraint.activate([
tabBar.heightAnchor.constraint(equalToConstant: 40),
tabBar.bottomAnchor.constraint(equalTo: view.bottomAnchor),
tabBar.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tabBar.widthAnchor.constraint(equalTo: view.widthAnchor)
])

delegate = self
transitionStyle = .horizontalStrip
arrangedObjects = ["0","1","2","3","4","5","6","7"]
selectedIndex = 0
view.wantsLayer = true

// register for mouse events
let trackingArea = NSTrackingArea(rect: view.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways, .inVisibleRect], owner: self, userInfo: nil)
view.addTrackingArea(trackingArea)
}
}

// NSPageController Delegate
extension PageController: NSPageControllerDelegate {

func pageController(_ pageController: NSPageController, frameFor object: Any?) -> NSRect {
return NSInsetRect(view.frame, -1, 0)
}

func pageController(_ pageController: NSPageController, identifierFor object: Any) -> String {
return (object as? String)!
}

func pageController(_ pageController: NSPageController, viewControllerForIdentifier identifier: String) -> NSViewController {
return ViewController(id: identifier)
}

func pageControllerWillStartLiveTransition(_ pageController: NSPageController) {
Swift.print("pageControllerWillStartLiveTransition")
addTabBar(withAnimation: false)
}

func pageControllerDidEndLiveTransition(_ pageController: NSPageController) {
pageController.completeTransition()
addTabBar(withAnimation: false)
Swift.print("pageControllerWillEndLiveTransition")
}
}

// tabBar functions
extension PageController {
func addTabBar(withAnimation shouldAnimate: Bool) {
view.addSubview(tabBar)
tabBar.frame.size.width = view.frame.size.width

if mouseIsInside {
tabBar.showWithAnimation(shouldAnimate)
}
}

func removeTabBar(withAnimation shouldAnimate: Bool) {
tabBar.hideWithAnimation(shouldAnimate)
}
}

// Mouse Movements
extension PageController {

override func mouseEntered(with event: NSEvent) {
mouseIsInside = true
addTabBar(withAnimation: true)
}

override func mouseExited(with event: NSEvent) {
mouseIsInside = false
removeTabBar(withAnimation: true)
}
}

提前致谢!

最佳答案

这似乎是一个 Unresolved 错误。这对我来说是固定的:

  1. 如果 pageController 填满了窗口的 View ,则将 contentView 的背景设置为 nil。这样,我们看到的背景始终是 pageController 的背景。

  2. 在此方法中对 View 进行排序:

    func pageControllerDidEndLiveTransition(_ pageController: NSPageController) {
    let controller = selectedViewController as! (YOUR_NSVIEWCONTROLLER_CLASS)
    controller.view.superview?.addSubview(controller.view, positioned: .below, relativeTo: TOOLBAR)
    }

YOUR_NSVIEWCONTROLLER_CLASS 替换为您的 ViewController 类名称,然后将 TOOLBAR 替换为您想要在顶部看到的 View 。

关于cocoa - NSView 覆盖 NSPageController 在转换时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40747362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com