Home > AI > IOS > UIKit >

UINavigationBarAppearance

configureWithOpaqueBackground

backgroundColor

Bar underline

.shadowColor = .clear // empty the under black line

Title text

let titleAttr: [NSAttributedString.Key: Any] = [
        .foregroundColor: UIColor.white
    ]
// to Large title
coloredAppearance.largeTitleTextAttributes = titleAttr
// to inline title
coloredAppearance.titleTextAttributes = titleAttr

Back button

let backButtonAppearance = UIBarButtonItemAppearance()
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]

let barAppearance = UINavigationBarAppearance()
barAppearance.backButtonAppearance = backButtonAppearance
UINavigationBar.appearance().scrollEdgeAppearance = barAppearance

Example

func initializeNavigationBarAppearance() {
    let barAppearance = UINavigationBarAppearance()
    // 设置为不透明
    barAppearance.configureWithOpaqueBackground()

    barAppearance.backgroundColor = .orange
    
    
    // discard bar underline, nil and clear are same
    barAppearance.shadowColor = .clear
    
    
    // title text
    let titleAttr: [NSAttributedString.Key: Any] = [
        .foregroundColor: UIColor.white
    ]
    barAppearance.largeTitleTextAttributes = titleAttr // large mode
    barAppearance.titleTextAttributes = titleAttr // inline mode
    

    
    // back button
    let backButtonAppearance = UIBarButtonItemAppearance()
    backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    barAppearance.backButtonAppearance = backButtonAppearance

    
    // back button image
    let backImage = UIImage(systemName: "pencil")?.withRenderingMode(.alwaysOriginal)
    barAppearance.setBackIndicatorImage(backImage, transitionMaskImage: backImage)
    
    
    // navigationBarTitle(displayMode: large)
    UINavigationBar.appearance().scrollEdgeAppearance = barAppearance
    
    // navigationBarTitle(displayMode: inline)
//    UINavigationBar.appearance().standardAppearance = barAppearance
     
}
Relevant tags:

Leave a Reply