Saturday, October 14, 2023
HomeiOS Developmentios - Can SwiftUI `ToolbarItem` buttons respect `UINavigationBarAppearance` attributes?

ios – Can SwiftUI `ToolbarItem` buttons respect `UINavigationBarAppearance` attributes?


I’m growing a library that may current SwiftUI views inside an app. I would like my navigation bar components to respect any look proxy configuration of the app. In a previous implementation utilizing UIKit, bar buttons routinely used any look proxy configuration (i.e. the font, together with coloration). This is what I am observing. Observe that “My Button” just isn’t styled.

enter image description here

The simplified code:

struct ContentView: View {
    var physique: some View {
        NavigationView {
            VStack {
                Textual content("Howdy, world!")
            }
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    
                    Button {
                        // do one thing
                    } label: {
                        Textual content("My Button")
                    }

…do not show the type. It does appear that SwiftUI respects navigation bar background coloration and title textual content attributes specified via the looks proxy mechanism. Nevertheless, this does not work for buttons.

static func configureAppearance() {
        
    UINavigationBar.look().tintColor = .blue
    
    let navigationBarAppearance = UINavigationBarAppearance()
        
    navigationBarAppearance.backgroundColor = .blue
                
    var titleTextAttributes = [NSAttributedString.Key: Any]()
    titleTextAttributes[NSAttributedString.Key.font] = UIFont(identify: "Menlo", measurement: 14)
    titleTextAttributes[NSAttributedString.Key.foregroundColor] = UIColor.white
        
    navigationBarAppearance.titleTextAttributes = titleTextAttributes

    let plainBarButtonItemAppearance = UIBarButtonItemAppearance(type: .plain)
    plainBarButtonItemAppearance.regular.titleTextAttributes = titleTextAttributes
    
    navigationBarAppearance.buttonAppearance = plainBarButtonItemAppearance
    
    let doneBarButtonItemAppearance = UIBarButtonItemAppearance(type: .carried out)
    doneBarButtonItemAppearance.regular.titleTextAttributes = titleTextAttributes
    navigationBarAppearance.doneButtonAppearance = doneBarButtonItemAppearance
    
    UINavigationBar.look().standardAppearance = navigationBarAppearance
    UINavigationBar.look().scrollEdgeAppearance = navigationBarAppearance
    UINavigationBar.look().compactAppearance = navigationBarAppearance
}

Is there some easy strategy to instruct SwiftUI to permit buttons to take part within the look proxy configuration?

I understand that I may manually learn attributes from the UINavigationBar.look() and assemble the analogous customizations in SwiftUI, however this appears unnecessarily tedious.

Utilizing Xcode 14.3 on iOS 16.4



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments