简体   繁体   English

如何将bar导航栏的位置更改为顶部

[英]How to change barPosition of navigationBar to topAttached in swift3

I need to change barPosition of navController's navBar to topAttached type. 我需要将navController的navBar的barPosition更改为topAttached类型。 I try to use position(for bar:) method of UINavigationBarDelegate. 我尝试使用UINavigationBarDelegate的position(for bar :)方法。 In MyViewController class: 在MyViewController类中:

navigationController?.navigationBar.delegate = self

and in extension: 并扩展为:

extension MyViewController: UINavigationBarDelegate{
    func position(for bar: UIBarPositioning) -> UIBarPosition {
        return .topAttached
    }
}

But it's not working, because of 但这不起作用,因为

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法在控制器管理的UINavigationBar上手动设置委托。”

So is here any else way to change barPosition in swift 3? 那么这里还有其他方法可以在Swift 3中更改barPosition吗? Thanks in advance! 提前致谢!

NavigationBar.position is a readonly property. NavigationBar.position是一个readonly属性。 You can't change it. 您无法更改。 If you want to change the frames of Navigationbar. 如果要更改导航栏的框架。 Firstly change the frames of UINavigationController and then bar in it. 首先更改UINavigationController的框架,然后将其插入。

Example: 例:

If you want to shift it downwards: 如果要向下移动它:

    self.navigationController?.view.frame = CGRect(x: 0.0, y: 100.0, width: 320.0, height: 426.0)
    self.navigationController?.navigationBar.frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 44.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM