iOS 15 导航栏透明化

165
我的iOS应用程序使用storyboard来设计用户界面,并使用自定义色调作为导航栏背景颜色。
我在Xcode 13 beta 5上测试了我的应用程序,发现导航栏是“白色的”,导航栏上的文本不可见。
在苹果开发者论坛https://developer.apple.com/forums/thread/682420中指出:“在iOS 15中,UIKit扩展了scrollEdgeAppearance的使用,该属性默认产生透明背景,适用于所有导航栏。” 要恢复旧的外观,您必须采用新的UINavigationBar外观API。
我添加了上述代码(来自上面的链接)到App Delegate的“application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions”方法中。
        if #available(iOS 13, *) {
            let navigationController = UINavigationController(navigationBarClass: nil, toolbarClass: nil)
            let navigationBar = navigationController.navigationBar
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = UIColor(red: 0.0/255.0, green: 125/255.0, blue: 0.0/255.0, alpha: 1.0)
            navigationBar.standardAppearance = appearance;
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
            navigationBar.isTranslucent = false
        }

这并没有解决问题。我仍然在导航栏的storyboard编辑器中设置了自定义色调。我是需要删除自定义色调还是我的外观API实现有误?


你在代码片段中创建了一个新的UINavigationController实例。你打算用这个实例做什么?只是猜测:我认为你正在寻找类似于UINavigationBar.appearance()的东西,但老实说,我不熟悉新的iOS 15 API。 - Teetz
https://developer.apple.com/forums/thread/682420 - Trenton McKinney
19个回答

2

我在iOS 15和旧版本中实现了导航栏配置的不透明和半透明:

extension UINavigationBar {
static let defaultBackgroundColor = UIColor.red
static let defaultTintColor = UIColor.white

func setOpaque() {
    if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UINavigationBar.defaultBackgroundColor
        appearance.titleTextAttributes = [.foregroundColor: UINavigationBar.defaultTintColor]
        
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    } else {
        setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.defaultPrompt)
        shadowImage = UIImage()
        barTintColor = UINavigationBar.defaultBackgroundColor
        titleTextAttributes = [.foregroundColor: UINavigationBar.defaultTintColor]
    }
    isTranslucent = false
    tintColor = UINavigationBar.defaultTintColor
}

func setTranslucent(tintColor: UIColor, titleColor: UIColor) {
    if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.titleTextAttributes = [.foregroundColor: titleColor]
        standardAppearance = appearance
        scrollEdgeAppearance = appearance
    } else {
        titleTextAttributes = [.foregroundColor: titleColor]
        setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        shadowImage = UIImage()
    }
    isTranslucent = true
    self.tintColor = tintColor
}

}


2
AppDelegate.swift文件中
window?.backgroundColor = .white

在我的情况下有效。

1

如果您想设置自定义返回按钮而不带标题和透明导航栏,则可以使用以下版本:

let backImg: UIImage = #imageLiteral(resourceName: "back")

if #available(iOS 15, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.titleTextAttributes = [.foregroundColor: UIColor.black]
    appearance.setBackIndicatorImage(backImg, transitionMaskImage: backImg)
    appearance.backButtonAppearance.normal.titlePositionAdjustment =
    UIOffset(horizontal: -1000.0, vertical: 0)
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
}

1

Objective-C 代码:将此代码实现在您的 viewDidLoad 函数中。


if (@available(iOS 15, *)){
    UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
    [appearance configureWithOpaqueBackground];
    appearance.titleTextAttributes = @{NSForegroundColorAttributeName : UIColor.blackColor};
    appearance.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:125/255.0 blue:0.0/255.0 alpha:1.0];
    self.navigationController.navigationBar.standardAppearance = appearance;
    self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
}

1
如果我们需要更改背景颜色和选中和未选中项目的颜色,只有这段代码适用于我。
我已经使用了这个来改变项目外观 tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
  if #available(iOS 15.0, *) {
        
        let tabBarAppearance = UITabBarAppearance()
        let tabBarItemAppearance = UITabBarItemAppearance()
        
        tabBarAppearance.backgroundColor = .white
        
        tabBarItemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: Constants.Color.appDefaultBlue]
        tabBarItemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
        tabBar.standardAppearance = tabBarAppearance
        tabBar.scrollEdgeAppearance = tabBarAppearance
        
    }

确保我们在TabBar类中使用此代码以获得所需的结果,如果我们在AppDelegate中使用它来设置外观,则可能无法正常工作。


1

如果只使用Storyboard来完成,可以在标题属性中选择“自定义”选项,以设置自定义标题文本属性。

滚动边缘标题属性


0

这段代码可以放在任何地方,不仅仅是在应用程序委托中,以解决iOS15上的问题:

                if #available(iOS 15, *) {
                
                let appearance = UINavigationBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundColor = <desired UIColor>
                navigationBar.standardAppearance = appearance;
                navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
                }

0
像这样做:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .red
appearance.titleTextAttributes = [.font: 
UIFont.boldSystemFont(ofSize: 20.0),
                              .foregroundColor: UIColor.white]

// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance

我写了一篇新文章谈论它。

https://medium.com/@eduardosanti/uinavigationbar-is-black-on-ios-15-44e7852ea6f7


0

我已经编辑了@Charlie Seligman分享的代码,因为它在我的一个屏幕中有一个滚动视图时无法正常工作。 即使您有滚动视图和导航栏,下面的代码也可以正常工作。

if #available(iOS 15, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            appearance.backgroundColor = UIColor(red: 0.89, green: 0.06, blue: 0.00, alpha: 1.00)
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        }

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