Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI, In toolbar, the size of label image is different between Menu and Button

I added two ToolbarItem. One is Menu and the other is Button.

.toolbar {
    // Menu 
    ToolbarItem(placement: .navigationBarTrailing) {
        Menu(content: {
            Text("hello world")
        }, label: {
            Image(systemName: "gear")
        })
    }
    
    // Button
    ToolbarItem(placement: .navigationBarTrailing) {
        Button(action: {
            print("button touched")
        }, label: {
            Image(systemName: "gear")
        })
    }
}

Even if I use same image as a label, the size is different. The result is like this.

enter image description here

I want to make menu label image size big as button, so that user can touch it more easily.

like image 654
naljin Avatar asked Jul 17 '21 12:07

naljin


2 Answers

Just add .imageScale(.large) modifier to the Image of the Menu

Menu(content: {
    Text("hello world")
}, label: {
    Image(systemName: "gear")
        .imageScale(.large) // Add this modifier

It looks like by default SwiftUI engine is figuring out the size to use on your behalf.

like image 188
cedricbahirwe Avatar answered Oct 24 '22 01:10

cedricbahirwe


works well on macos 12.beta, xcode 13.beta, target ios 15 and macCatalyst. This is the code I used for testing:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("testing")
                .toolbar {
                    // Menu
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Menu(content: {
                            Text("hello world")
                        }, label: {
                            Image(systemName: "gear")//.resizable().scaleEffect(1.2)
                        })
                    }
                    // Button
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {
                            print("button touched")
                        }, label: {
                            Image(systemName: "gear")
                        })
                    }
                }
        }
    }
}

You could use "Image(systemName: "gear").resizable().scaleEffect(x)" to iron out any mismatch.

like image 24
workingdog support Ukraine Avatar answered Oct 24 '22 00:10

workingdog support Ukraine