How can I fix the navigationbar title position in

2019-07-27 12:02发布

问题:

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!