在iOS11中,导航栏标题背景为白色。

3

iOS 11中,导航栏标题标签出现白色背景,原因不明:

navigation bar with white background for title

这只是一个普通的导航控制器,标题是按默认方式设置的:
self.title = @"My Title";

这个问题只发生在iOS 11中,屏幕截图是从模拟器中获取的,之前的iOS版本都没有问题。
有什么建议可以让我拥有一个正常的透明标签或者移除那个无缘无故出现的白色背景?
1个回答

2
请检查您是否正确设置了标题文本属性。您可以从Storyboard或编程方式设置标题文本属性。选择Storyboard -> NavigationBar -> 转到属性检查器 -> 标题文本属性

enter image description here

或在ViewController中手动设置

Swift 4:

         self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white,
            NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17, weight: .bold)
        ]

Swift 3:

        self.navigationController?.navigationBar.titleTextAttributes = [
            NSForegroundColorAttributeName: UIColor.white,
            NSFontAttributeName: UIFont.systemFont(ofSize: 17, weight: .bold)
        ]

这并不完全是我的问题解决方案,但它确实帮助我搜索并找出了问题,所以我接受了这个答案。我正在修改别人编写的代码,其中使用了一个UINavigationBar类别,其中包含设置titleTextAttributes的代码,但它将前景和背景都设置为白色,因此我将背景设置为透明,现在它可以正常工作了,感谢您的帮助。 - Azhar Ali
很好,将其保留在UINavigationController或NavigationBar扩展中是个好主意。我认为您不需要设置backgroundColor属性,因为它的默认值为nil。https://developer.apple.com/documentation/foundation/nsattributedstringkey/1528791-backgroundcolor - Suhit Patil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接