Navigation Bar title font problem on ios 13

前端 未结 2 711
陌清茗
陌清茗 2021-01-25 17:05

I’m using Xcode 11.4 and iOS 13.4. I have set navigation bar title custom font using UINavigatinBar.appearance() And it works correctly but on iOS 13+ when i try to push to anot

相关标签:
2条回答
  • 2021-01-25 17:53

    Here you go, manage it in viewDidAppear:

    let lblTitle = UILabel()
    
    let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
                                                    .foregroundColor: UIColor.black]
    
    let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
    
    lblTitle.attributedText = attributeString
    
    lblTitle.sizeToFit()
    navigationItem.titleView = lblTitle
    
    0 讨论(0)
  • 2021-01-25 17:59

    iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems

    Check this code, might help you

        let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
        let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
    
        UINavigationBar.appearance().tintColor = UIColor.white // bar icons
    
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .red // If you want different nav background color other than white
    
            appearance.titleTextAttributes = titleFontAttrs
            appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
    
            UINavigationBar.appearance().isTranslucent = false
    
            appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
            appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
    
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            UINavigationBar.appearance().barTintColor = .red // bar background
    
            UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
    
            UINavigationBar.appearance().isTranslucent = false
    
            UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
            UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
        }
    
    0 讨论(0)
提交回复
热议问题