How can I fix the navigationbar title position in ios11

a 夏天 提交于 2019-12-11 06:06:03

问题


Here is the code

[self presentViewController:viewController animated:YES completion:nil];

viewController is a UIImagePickerController

When I presented in viewController, navigationbar title shifted.

Please check the image.

Does anyone know how can I fix it?


回答1:


Using the following method to customize and change navigation bar button

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
 {
          //---> Code
 }

For more info seen the following link: Link




回答2:


You need to give a constraint to titleView, from iOS11 navigation bar is playing with constraint instead of frames like before so,

public extension UIView {
//From iOS 11 -> UIBarButtonItem uses autolayout instead of dealing with frames. so adding contraint for the barbuttonItem
public func applyNavBarConstraints(size: (width: CGFloat, height: CGFloat)) {
    if #available(iOS 11.0, *) {
        let widthConstraint = self.widthAnchor.constraint(equalToConstant: size.width)
        let heightConstraint = self.heightAnchor.constraint(equalToConstant: size.height)
        heightConstraint.isActive = true
        widthConstraint.isActive = true
    }
}}

and use this as below:

navigationItem.titleView.applyNavBarConstraints(size: (width: viewWidth, height: viewHeight))

Hope this helps!



来源:https://stackoverflow.com/questions/47830649/how-can-i-fix-the-navigationbar-title-position-in-ios11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!