9

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

它在 iOS 10 之前工作正常,但在 iOS 11 中,导航标题视图放错了位置。

这是 iOS 10 的屏幕截图 -

标题视图看起来不错

这是 iOS 11 的屏幕截图 -

标题视图下移

正如您在屏幕截图中看到的那样,当我在 iOS 10 上运行代码时,标题视图似乎很好。但是 iOS 11 上的相同代码将标题视图向下移动了一些像素并被剪切。

这就是我设置标题视图的方式 -

navigationItem.titleView = MY_CUSTOM_TITLE_VIEW

我尝试了很多东西并搜索了许多解决方案,但没有任何效果。

4

2 回答 2

17

这是如何修复它 -

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

override var intrinsicContentSize: CGSize {
    return UILayoutFittingExpandedSize
}

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

于 2017-10-06T09:13:48.557 回答
2

当您将自定义视图添加到标题视图时,适用于 iOS 的新导航栏存在问题。因此,您只需在实现导航栏自定义之前添加“prefertsLargeTitles”为 No 和“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]];

希望能帮到你^_^

于 2017-10-05T17:44:31.843 回答