将项目后面的子视图添加到iOS 11中的UINavigationBar

时间:2017-08-14 09:01:35

标签: swift xcode uinavigationbar ios11

在我的UINavigationBar子类中,我添加了一个带有insertSubview(customView, at: 0)的子视图,以便将其放在UINavigationBar的“后面”。

在iOS 10中,添加的子视图将添加标题和UIBarButtonItems,因为我想要它,但在iOS 11中,似乎Apple改变了一些东西,因为无论我尝试customView将是什么每次都放在UINavigationBar

的前面
//containerView
containerView.frame = CGRect(x: 0, y: -(statusBarHeight), width: UIScreen.main.bounds.width, height: frame.height + statusBarHeight)
containerView.clipsToBounds = true

insertSubview(containerView, at: 0)

我已经尝试过更改索引了。

知道我应该如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

你可以看看这个。我这样解决了:

override func layoutSubviews() {  
        super.layoutSubviews()  
        for aView in self.subviews {  
            if NSStringFromClass(type(of: aView)) == "_UINavigationBarContentView" {  
                aView.frame = CGRect(x: 0, y: 20, width: aView.frame.width, height: aView.frame.height)  
            }  
            else if NSStringFromClass(type(of: aView)) == "_UIBarBackground" {  
                aView.frame = CGRect(x: 0, y: 0, width: aView.frame.width, height: self.frame.height)  
            }  
        }  
    }