SwiftUI:让按钮的左右保持在 parent 的左右
SwiftUI: Have button's right and left stick to parent's right and left
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)
}
}
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)
}
}
}
}
你试过了吗.frame(minWidth: 0, maxWidth: .infinity)
将框架宽度设置为 ".infinity" 使其使用最大宽度
SwiftUI Button fill parent
这是完整的代码 -
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()
}
}
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)
}
}
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)
}
}
}
}
你试过了吗.frame(minWidth: 0, maxWidth: .infinity)
将框架宽度设置为 ".infinity" 使其使用最大宽度
SwiftUI Button fill parent
这是完整的代码 -
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()
}
}