为什么正文中的按钮与我的 NavigationView 工具栏中的按钮的行为不同?

尼莫

我真的不明白为什么下面的按钮:

Button { } label: {
    Image(systemName: "globe")
         .resizable()
         .scaledToFill()
}
.frame(width: size, height: size)
.border(.yellow)

body在和 中的行为是否不同NavigationView为什么没有NavigationView填充整个框架?

struct ContentView: View {
    let size = 45.0
    let fontSize = 17.0
    
    var body: some View {
        NavigationView {
            Button { } label: {
                Image(systemName: "globe")
                    .resizable()
                    .scaledToFill()
            }
            .frame(width: size, height: size)
            .border(.yellow)
            .toolbar {
                HStack(alignment: .top) {
                    
                    Button { } label: {
                        Image(systemName: "globe")
                            .resizable()
                            .scaledToFill()
                    }
                    .frame(width: size, height: size)
                    .border(.red)
                    
                    // Image and Title
                    VStack {
                        Image("nemo")
                            .resizable()
                            .scaledToFit()
                            .frame(height: size)
                        Text("Navigation Title")
                            .font(.system(size: fontSize))
                            .bold()
                    }.border(.green)
                }
                //.frame(maxWidth: .infinity)
            }
        }
    }
}

在此处输入图像描述

粗糙的

这是工具栏中默认按钮的设计,我们可以使用自定义,例如

    Button { print(">> action here")} label: {
        Image(systemName: "globe")
            .resizable()
            .scaledToFill()
    }
    .buttonStyle(MyTabbarButtonStyle())
    .frame(width: size, height: size)
    .border(.red)

// and style (tune colours as you want)
struct MyTabbarButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .foregroundColor(configuration.isPressed ? .gray : .blue)
    }
}

演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章