iOS 11 Navigation TitleView misplaced

限于喜欢 提交于 2019-11-30 20:10:03

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.

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

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