覆盖NSPageController的NSView在转换时消失

问题描述 投票:2回答:1

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

我将上栏和下栏添加到视图中,同时在NSPageControllers viewDidLoad()方法中添加约束条件。

它们在第一个页面上显示得很好,但是当我开始从一个页面过渡到另一个页面时,新的NSViewController被重新绘制在覆盖的视图上,它们消失了。我可以验证它们是在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)
    }
}

先谢谢你!

cocoa drawing nsview nsviewcontroller
1个回答
0
投票

这似乎是一个没有解决的bug。这对我来说是固定的。

  1. 如果... pageController 充满了窗口的视图,设置为零,则 contentView的背景。这样一来,我们看到的背景就永远是那个 pageController.

  2. 用这种方法对视图进行排序。

    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 为你想在上面看到的景色。

© www.soinside.com 2019 - 2024. All rights reserved.