2017-10-05 65 views
5

我有一个iOS应用程序,我在其中设置自定义导航标题视图。iOS 11导航标题查看错位

它工作正常,直到iOS 10,但在iOS 11导航标题视图是错位。

这里是截屏为iOS 10 -

The title view looks fine

这里是截屏为iOS 11 -

The title view is shifted down

正如你所看到的当我在iOS 10上运行代码时,屏幕截图显示标题视图没问题。但是,iOS 11上的相同代码会将标题视图向下移动一些像素,并且会被剪切掉。

这是我如何设置标题视图 -

navigationItem.titleView = MY_CUSTOM_TITLE_VIEW

我试了很多东西,并寻找多种解决方案,但没有什么工作。

+0

可能重复的[iOS 11 navigationItem.titleView宽度未设置](https://stackoverflow.com/questions/44932084/ios-11-navigationitem-titleview-width-not-set) – Dania

回答

13

下面是它的可固定 -

在自定义标题视图类添加该代码 -

override var intrinsicContentSize: CGSize { 
    return UILayoutFittingExpandedSize 
} 

而自定义标题视图显示在正确的位置。

+0

它适合我。但是如果我只有左/右栏按钮项目,标题视图将不会显示在绝对中心水平。 – Lumialxk

1

当您将自定义视图添加到标题视图时,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]]; 

希望能帮助你^ _^

+0

感谢您的答案,但它没有为我工作。然而,我尝试了一些,它的工作。 –

+0

@PrateekVarshney也许分享你试过的那个工作?! –

+0

是的,我已经将它添加为答案 –