iOS 11 Navigation TitleView misplaced
Asked Answered
M

2

9

I have an iOS app in which I am setting a custom navigation title view.

It was working fine till iOS 10, but in iOS 11 the navigation title view is misplaced.

Here is the screen shot for iOS 10 -

The title view looks fine

Here is the screen shot for iOS 11 -

The title view is shifted down

As you can see in the screen shots that when I run the code on iOS 10 the title view appears to be fine. But the same code on iOS 11 shifts the title view down by some pixels and it gets cut.

This is how I am setting the title view -

navigationItem.titleView = MY_CUSTOM_TITLE_VIEW

I tried many things and searched for many solutions but nothing is working.

Merta answered 5/10, 2017 at 5:54 Comment(0)
M
17

Here's how it can be fixed -

Add this code in the custom title view class -

override var intrinsicContentSize: CGSize {
    return UILayoutFittingExpandedSize
}

And the custom title view shows up at the correct position.

Merta answered 6/10, 2017 at 9:13 Comment(2)
It works for me. But if I only have left/right bar button item, the title view will not show at absolute center horizontal.Aboard
Swift 5: return UIView.layoutFittingExpandedSizePtyalin
C
2

There are problem with new Navigation Bar for iOS, when you add custom view into title view. So, you just add "prefertsLargeTitles" is No & "largeTitleDisplayMode" is DisplayModeNever before implement navigation bar custom.

Here 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 ^_^

Confucius answered 5/10, 2017 at 17:44 Comment(4)
Thanks for your answer but it didn't work for me. However I tried something and it worked.Merta
@PrateekVarshney Maybe share what you tried that worked?!Heyman
Yes I have added it as an answerMerta
it's not a "problem" in iOS 11, it's just that in iOS 11 the navigationBar uses AutoLayout, whereas before it didn't.Bilingual

© 2022 - 2024 — McMap. All rights reserved.