Xcode 11.4. Navigation's Title Color gone BLACK from storyboard

前端 未结 7 965
挽巷
挽巷 2020-12-08 06:05

I recently updated my Xcode to 11.4. When I run the app on the device, i\'ve noticed that all my navigations item\'s titles gone fully black when being set from storyboard.

7条回答
  •  不思量自难忘°
    2020-12-08 06:46

    This fixed it for me, using UINavigationBarAppearance instead, from: Customizing Your App’s Navigation Bar

    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.black
        appearance.titleTextAttributes = [.foregroundColor: UIColor.white] // With a red background, make the title more readable.
        self.navigationBar.standardAppearance = appearance
        self.navigationBar.scrollEdgeAppearance = appearance
        self.navigationBar.compactAppearance = appearance // For iPhone small navigation bar in landscape.
    } else {
        self.navigationBar.barTintColor = UIColor.black
        self.navigationBar.tintColor = UIColor.white
        self.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
    }
    

    Note: I subclassed UINavigationController, and this was called from the override of viewWillAppear.

    ...or for AppDelegate, app-wide:

    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.black
        appearance.titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
    
        let buttonAppearance = UIBarButtonItemAppearance()
        buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
        appearance.buttonAppearance = buttonAppearance
    
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
        UINavigationBar.appearance().compactAppearance = appearance
    
        UIBarButtonItem.appearance().tintColor = UIColor.white
    } else {
        UINavigationBar.appearance().barTintColor = UIColor.black
        UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
        UINavigationBar.appearance().tintColor = UIColor.white
    
        UIBarButtonItem.appearance().tintColor = UIColor.white
    }
    

    ...for AppDelegate, app-wide, in Objective-C:

    if (@available(iOS 13, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor = UIColor.whiteColor;
        appearance.titleTextAttributes = titleAttributes;
    
        UIBarButtonItemAppearance *buttonAppearance = [[UIBarButtonItemAppearance alloc] init];
        buttonAppearance.normal.titleTextAttributes = barButtonItemAttributes;
        appearance.buttonAppearance = buttonAppearance;
    
        UINavigationBar.appearance.standardAppearance = appearance;
        UINavigationBar.appearance.scrollEdgeAppearance = appearance;
        UINavigationBar.appearance.compactAppearance = appearance;
    
        [[UINavigationBar appearance] setTintColor:UIColor.blackColor];
    } else {
        [[UINavigationBar appearance] setBarTintColor:UIColor.whiteColor];
        [[UINavigationBar appearance] setTintColor:UIColor.blackColor];
        [[UINavigationBar appearance] setTranslucent:false];
        [[UINavigationBar appearance] setTitleTextAttributes: titleAttributes];
        [[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemAttributes forState:UIControlStateNormal];
    }
    

提交回复
热议问题