SwiftUI问题在这里...
我正在尝试对按钮进行布局,以使其具有屏幕的整个宽度减去16的填充。我不想使用此UIScreen.main.bounds.width。我希望它是动态的。
你们有什么想法吗?
谢谢!
代码示例
通过使用静态值可以正常工作
struct TestButton : View {
var body: some View {
Button(action: {
}) {
Text("Tap me")
}
.modifier(PrimaryButton())
}
}
fileprivate struct PrimaryButton: ViewModifier {
func body(content: Content) -> some View {
content
.frame(width: 300, height: 28)
.padding()
.background(Color.yellow)
.foregroundColor(Color.white)
}
}
使用dfd的注释,不会更改任何内容。
struct TestButton : View {
var body: some View {
Button(action: {
}) {
Text("Tap me")
}
.modifier(PrimaryButton())
}
}
fileprivate struct PrimaryButton: ViewModifier {
func body(content: Content) -> some View {
content
.relativeWidth(1.0)
.padding()
.background(Color.yellow)
.foregroundColor(Color.white)
}
}
答案 0 :(得分:2)
GeometryReader可能会帮助您
例如:
SomeButton: View {
var body: some View {
GeometryReader { geometry in
VStack() {
Button(action:{}) {
Text("\(geometry.size.width)")
}.padding()
.frame(minWidth: geometry.frame(in: .global).size.width,
minHeight: geometry.frame(in: .global).size.height
)
.background(Color.red)
}
}
}
}
答案 1 :(得分:0)
您是否尝试过.frame(minWidth: 0, maxWidth: .infinity)
将框架宽度设置为“。infinity” 使其使用最大宽度
这是完整的代码-
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading, spacing: CGFloat(5)){
Button(action: {}) {
Text("Button fill width")
.font(.title)
.foregroundColor(.white)
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.gray)
HStack(alignment: .center, spacing: CGFloat(5)){
Button(action: {}) {
Text("Btn-1")
.font(.title)
.foregroundColor(.white)
}.padding()
.background(Color.gray)
Button(action: {}) {
Text("Button fill width")
.font(.title)
.foregroundColor(.white)
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.gray)
}
HStack(alignment: .center, spacing: CGFloat(5)){
Button(action: {}) {
Text("B-1")
.font(.title)
.foregroundColor(.white)
}.padding()
.background(Color.gray)
Button(action: {}) {
Text("B fill")
.font(.title)
.foregroundColor(.white)
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.gray)
Button(action: {}) {
Text("B fill")
.font(.title)
.foregroundColor(.white)
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.gray)
}
HStack(alignment: .center, spacing: CGFloat(5)){
Spacer()
Text("Hello World...")
.font(.title)
.fontWeight(.heavy)
.frame(minWidth: 0, maxWidth: .infinity)
.padding(5)
}.background(Color.gray)
HStack(alignment: .center, spacing: CGFloat(5)){
Spacer()
Text("Hello World...")
.font(.body)
.fontWeight(.heavy)
.multilineTextAlignment(.leading)
.lineLimit(10)
.padding(5)
}.background(Color.gray)
HStack(alignment: .center, spacing: CGFloat(5)){
Button(action: {}) {
Text("1").foregroundColor(.black).bold()
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background( Image("RC_btn_default_normal")
.renderingMode( Image.TemplateRenderingMode.original))
Button(action: {}) {
Text("2").foregroundColor(.black).bold()
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background( Image("RC_btn_default_normal")
.renderingMode( Image.TemplateRenderingMode.original))
Button(action: {}) {
Text("3").foregroundColor(.black).bold()
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(Image("RC_btn_default_normal")
.renderingMode( Image.TemplateRenderingMode.original))
Button(action: {}) {
Text("4").foregroundColor(.black).bold()
}.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background( Image("RC_btn_default_normal")
.renderingMode( Image.TemplateRenderingMode.original))
}
Spacer()
}.background(Color.green)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}