How can I fix the navigationbar title position in

2019-07-27 11:56发布

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.

I'm the image

Does anyone know how can I fix it?

2条回答
看我几分像从前
2楼-- · 2019-07-27 12:39

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

查看更多
一夜七次
3楼-- · 2019-07-27 12:55

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!

查看更多
登录 后发表回答