简体   繁体   中英

Prevent SwiftUI System Image from dynamically changing size for navigation bar item

I would like to add a plus button to the navigation bar items using the system plus image in SwiftUI. However, I am unable to prevent the system image from scaling dynamically when the accessibility font changes.

How can I stop it resizing so it acts like a standard UINavigationBarButtonItem system plus button?

The accessibility feature of holding on to a navigation bar button for large font types also doesn't work like it does with UIKit.

Really frustrating that a potentially simple thing can't be done with SwiftUI and that accessibility hasn't been thought about. The SwiftUI tutorials profile bar button also doesn't work for large font sizes. (PS SwiftUI is the future)

Here was my attempt:

struct ContentView : View {
    var body: some View {
        NavigationView {
            List {
                Text("Stop + Bar Button resizing")
                    .lineLimit(nil)
            }
            .navigationBarTitle(Text("Plus"))
            .navigationBarItems(trailing:
                PlusNavigationButton()
            )
        }
    }
}

struct PlusNavigationButton: View {
    var body: some View {
        PresentationButton(
            Image(systemName: "plus")
                .resizable()
                .frame(width: 44, height: 44),
            destination: NewView())
    }
}

加栏按钮图像动态调整大小

You should build what you exactly want. So if you want to use 16x16 image with some extra hitTest area, you can build like this:

var body: some View {
    PresentationButton(
        HStack() {
           Spacer()
           Image(systemName: "plus")
               .resizable()
               .frame(width: 16, height: 16)
           Spacer()
        }.frame(width: 44, height: 44),    
        destination: NewView()
    )
}

or if you like some space around your image and let it fill the rest, you can:

var body: some View {
    PresentationButton(
        Image(systemName: "plus")
            .resizable()
            .padding(14)
            .frame(width: 44, height: 44),  
        destination: NewView()
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM