IOS 11 Navigation TitleView is inappropriate

I have an iOS app in which I customize the appearance of the navigation header.

It worked fine until iOS 10, but in iOS 11 the appearance of the navigation header is inappropriate.

Here is a screenshot for iOS 10 -

The title view looks great

Here is a screenshot for iOS 11 -

Header view shifted down

As you can see in the screenshots, when you run the code on iOS 10, the header looks normal. But the same code on iOS 11 shifts the header view by some pixels and is cropped.

This is how I set the title view -

navigationItem.titleView = MY_CUSTOM_TITLE_VIEW

I tried many things and looked for many solutions, but nothing works.

+5
source share
2 answers

Here's how to fix it -

Add this code to your custom header view class -

 override var intrinsicContentSize: CGSize { return UILayoutFittingExpandedSize } 

And the custom header image is displayed in the correct position.

+13
source

Problem with the new navigation bar for iOS when adding a custom view to the title view. So you just add "prefertsLargeTitles": No and "largeTitleDisplayMode" - DisplayModeNever before you implement a custom navigation bar.

Here is my code:

 if (@available(iOS 11.0, *)) { [[self navigationController] navigationBar].prefersLargeTitles = NO; [[self navigationController] navigationItem].largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever; } // Add contraints to titleView NSLayoutConstraint *centerPrompt= [NSLayoutConstraint constraintWithItem:midPromptLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:midView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; NSLayoutConstraint *topPrompt= [NSLayoutConstraint constraintWithItem:midPromptLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:midView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]; NSLayoutConstraint *centerTitle= [NSLayoutConstraint constraintWithItem:midTitleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:midView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; NSLayoutConstraint *topTitle= [NSLayoutConstraint constraintWithItem:midTitleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:midPromptLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]; [midView addConstraints:@[centerPrompt,topPrompt,centerTitle,topTitle]]; 

Hope will help you ^ _ ^

+1
source

Source: https://habr.com/ru/post/1272358/


All Articles