iOS 11导航TitleView错位
我有一个iOS应用程序,我在其中设置自定义导航标题视图。
它工作正常,直到iOS 10,但在iOS 11的导航标题视图是错位的。
这里是iOS 10的屏幕截图 –
这里是iOS 11的屏幕截图 –
正如你在屏幕截图中看到的那样,当我在iOS 10上运行代码时,标题视图看起来很好。 但是,iOS 11上的相同代码将标题视图向下移动了一些像素,并被剪切掉。
这就是我如何设置标题视图 –
navigationItem.titleView = MY_CUSTOM_TITLE_VIEW
我尝试了很多东西,并寻找很多解决scheme,但没有任何工作。
这是如何修复的 –
在自定义标题视图类中添加此代码 –
override var intrinsicContentSize: CGSize { return UILayoutFittingExpandedSize }
自定义标题视图显示在正确的位置。
当您将自定义视图添加到标题视图时,iOS的新导航栏存在问题。 因此,在实现导航栏自定义之前,您只需添加“prefertsLargeTitles”是否“largeTitleDisplayMode”是DisplayModeNever。
在这里我的代码:
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]];
希望能帮助你^ _ ^