swiftui - 如何在 SwiftUI 中使视图宽度等于超级视图
问题描述
我有Button
struct ContentView : View {
var body: some View {
HStack {
Button(action: {}) {
Text("MyButton")
.color(.white)
.font(Font.system(size: 17))
}
.frame(height: 56)
.background(Color.red, cornerRadius: 0)
}
}
}
但我想把它固定在 superview 的边缘(拖到 superview 的尾随和前导)。像这样:
HStack
对我没有帮助,它正在期待。固定frame
或宽度等于UIScree.size
不是灵活的解决方案。
解决方案
你需要使用.frame(minWidth: 0, maxWidth: .infinity)
修饰符
添加下一个代码
Button(action: tap) {
Text("Button")
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.red)
}
.padding([.leading, .trailing], 20)
填充修饰符将允许您在边缘留出一些空间。
请记住,修饰符的顺序很重要。因为修饰符实际上是包装视图的函数(它们不会更改视图的属性)
推荐阅读
- racket - 分别计算每个子列表的匹配数
- javascript - 当屏幕宽度小于或大于
- java - 我的应用程序不断崩溃。我正在尝试像 instagram 一样做背景动画
- c++ - struct 是否比嵌入式系统中的类具有更好的性能
- python - Django:ModelForm 的 kwargs
- javascript - 一个组件中的表单被其他组件提交
- c - POSIX,在 pthread_t 之间传递应用程序数据
- android - 如何在不单击按钮的情况下验证密码?
- c# - 用正则表达式替换字符串中项目的特定实例
- php - 如何设置 phpmyadmin 和 SQL 以通过 REST-api 进行消息传递?