xcode 导航栏标题和导航按钮未出现在 iOS 11 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45751376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:11:39  来源:igfitidea点击:

Navigation bar title and navigation buttons not appearing on iOS 11

iosobjective-cxcodeios11xcode9-beta

提问by Leeroy Jenkins

Prior to iOS 11, the UINavigationBarbuttons and title are being displayed correctly.

在 iOS 11 之前,UINavigationBar按钮和标题显示正确。

Yesterday I downloaded Xcode 9 with iOS 11 and, after building and running without doing changes, both navigation buttons and the title are not being displayed anymore. It shows the UINavigationBarwith the correct color I am setting but nothing else.

昨天我用 iOS 11 下载了 Xcode 9,在没有做任何更改的情况下构建和运行后,导航按钮和标题都不再显示。它显示了UINavigationBar我正在设置的正确颜色,但仅此而已。

I tried on different simulators and also I updated an iPhone 7 to iOS 11 beta 5 and the result is the same. Nothing being displayed.

我尝试了不同的模拟器,并将 iPhone 7 更新到 iOS 11 beta 5,结果是一样的。什么都没有显示。

Has someone faced the same problem? I have tried changing different parts of the code and storyboard but nothing affects...

有人遇到过同样的问题吗?我尝试更改代码和故事板的不同部分,但没有任何影响......

EDIT with screenshots: http://imgur.com/a/Hy46c

用截图编辑:http: //imgur.com/a/Hy46c

Thanks in advance!

提前致谢!

回答by Justin Vallely

For Xcode 9, it appears that it is no longer enough to just set the frame of a custom view that is being injected into the navigationItem titleView. The intrinsic content size of your titleView now must be overriden and set as well.

对于 Xcode 9,似乎仅仅设置被注入到 navigationItem titleView 的自定义视图的框架已经不够了。现在,您的 titleView 的内在内容大小也必须被覆盖和设置。

Here's the code, adjust the width and height to suit your needs:

这是代码,根据您的需要调整宽度和高度:

class NavigationBarTitleView: UIView {

    override var intrinsicContentSize: CGSize {
        return CGSize(width: bounds.width - 100, height: 50)
    }

    ...

}

回答by Carly O'Keefe

use sizeToFit()! ios 11 automatically sizes it, but ios 10 does not

使用 sizeToFit()!ios 11 会自动调整大小,但 ios 10 不会

回答by Shawn

I found this code in some inherited codebase, commented it out and everything worked as it did before iOS 11.x.

我在一些继承的代码库中找到了这段代码,将其注释掉,一切都像 iOS 11.x 之前一样。

if (appDelegate.window.rootViewController != self) {
    appDelegate.window.rootViewController = self;
}

回答by rtamesis

I had the same problem in my project where the titles were missing from the navigation bars after updating to Xcode 9 and iOS 11. I solved it by going to the navigation bar of my navigation controller on the storyboard, keeping the Prefers Large Titles unchecked and changing the Title Font under Title Text attributes, which was set by default in Xcode 9 to System 0 to some other option like Caption 1 or Headline. I also changed its children viewcontrollers' navigation bar settings For Large Title to Never instead of Automatic or Always.

我在我的项目中遇到了同样的问题,更新到 Xcode 9 和 iOS 11 后,导航栏中的标题丢失了。我通过转到情节提要上导航控制器的导航栏解决了这个问题,保持未选中 Prefers Large Titles 和将 Title Text 属性下的 Title Font(在 Xcode 9 中默认设置为 System 0)更改为其他一些选项,如 Caption 1 或 Headline。我还将其子视图控制器的大标题导航栏设置更改为从不,而不是自动或始终。

回答by massimobio

I had the same issue and for me it was caused by subclassing UITabBarController

我有同样的问题,对我来说它是由子类化 UITabBarController 引起的

回答by Phong Cao

Did you set "window,rootViewController = ..." in your code ? Try remove it can fix your problem

您是否在代码中设置了“window,rootViewController = ...”?尝试删除它可以解决您的问题

回答by jerrygdm

Try to use:

尝试使用:

        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

or without appearance proxy setting directly to the current navigationBar...It solves my problem, should Apple changed titleText to clear as default in iOS11...?

或者没有直接对当前导航栏的外观代理设置......它解决了我的问题,Apple是否应该在iOS11中将titleText更改为默认清除......?

Also use this if you want the same look as iOS 10:

如果您想要与 iOS 10 相同的外观,也可以使用它:

        if #available(iOS 11, *) {
        nav.navigationBar.prefersLargeTitles = false
    }

回答by Alexander Telegin

Based on the Justin Vallely's comment I've reworked the code a little to ensure proper sizing of the view:

根据 Justin Vallely 的评论,我对代码进行了一些修改,以确保正确调整视图大小:

class NavigationBarTitleView: UIView {
    private var width: CGFloat = 0.0
    private var height: CGFloat = 0.0

    override init(frame: CGRect) {
        super.init(frame: frame)
        width = frame.width
        height = frame.height
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override var intrinsicContentSize: CGSize {
        return CGSize(width: width, height: height)
    }
}

In my particular case I've used this view as a container to UISearchBar and now it is well sized and worked perfectly with Swift 4 & iOS 11, just as it used to work on previous iOS & Swift versions

在我的特殊情况下,我使用这个视图作为 UISearchBar 的容器,现在它大小合适并且与 Swift 4 和 iOS 11 完美配合,就像它曾经在以前的 iOS 和 Swift 版本上工作一样

回答by Casey

We were facing the same issue where the navigation bar color is there but the title and the buttons are not showing up. We have double checked the bar was there by triggering a navigation bar background color change 2 seconds after the navigation controller showed up on the screen, so we know the navigation bar was there and we were adding buttons to the correct instance. Same as the OP, this issue only appears on iOS 11 and not iOS 10, and we are using Swift 3.2 running Xcode 9.1.

我们遇到了同样的问题,导航栏颜色在那里,但标题和按钮没有显示。我们通过在导航控制器出现在屏幕上 2 秒后触发导航栏背景颜色更改来仔细检查栏是否存在,因此我们知道导航栏在那里并且我们正在向正确的实例添加按钮。与 OP 相同,这个问题只出现在 iOS 11 而不是 iOS 10,我们使用的是运行 Xcode 9.1 的 Swift 3.2。

After hours of fiddling around, it turns out that presenting a navigation controller, then making it as the UIApplication.shared.delegate.window.rootViewController (after the present animation) caused the issue in our case.

经过几个小时的摆弄,事实证明,呈现一个导航控制器,然后将其作为 UIApplication.shared.delegate.window.rootViewController(在当前动画之后)导致了我们的案例中的问题。

If you just skip the present view controller and make the navigation controller as the root view controller, then everything works fine. Of course, you lose the present animation in the case.

如果您只是跳过当前视图控制器并将导航控制器设为根视图控制器,那么一切正常。当然,在这种情况下,您会丢失当前的动画。

回答by pereckeSokSzam

Had the same issue with the navigationButton not displayed. I solved it by setting the renderingMode to .alwaysOriginal. (I didn't use templates)

与未显示导航按钮的问题相同。我通过将 renderMode 设置为.alwaysOriginal. (我没有使用模板)

Swift 3 code:

斯威夫特 3 代码:

var img =R.image.smt()?.withRenderingMode(.alwaysOriginal)