简体   繁体   中英

How to change barPosition of navigationBar to topAttached in swift3

I need to change barPosition of navController's navBar to topAttached type. I try to use position(for bar:) method of UINavigationBarDelegate. In MyViewController class:

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.'

So is here any else way to change barPosition in swift 3? Thanks in advance!

NavigationBar.position is a readonly property. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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