SwiftUI 阻止 Divider 在 HStack 中垂直扩展
SwiftUI stop Divider from expanding vertically in HStack
我正在使用 SwiftUI 创建类似于警报弹出窗口的东西,我使用 UIHostingController 从 UIKit 代码中呈现它。视图如下所示:
VStack(spacing: 0) {
// Some text ...
HStack(spacing:0) {
Button(action: self.onCancel) { Text("Cancel") }
.padding().inExpandingRectangle().fixedSize(horizontal: false, vertical: true)
// This divider is the problem
Divider() // .fixedSize()
Button(action: self.onDelete) { Text("Delete") }
.padding().inExpandingRectangle().fixedSize(horizontal: false, vertical: true)
}
}.frame(minHeight: 0)
inExpandingRectangle
是我在另一个 Whosebug 问题中发现的。它将文本居中放置在 HStack 的每一侧。
extension View {
func inExpandingRectangle() -> some View {
ZStack {
Rectangle().fill(Color.clear)
self
}
}
}
看起来像这样。垃圾。
如果我将 .fixedSize()
放在分隔线上,它会执行此操作。不可怕,但分隔线看起来很愚蠢,不会扩展到按钮的大小。
在 HStack
上放置 fixedSize()
修饰符而不是 Divider
可以解决问题。
var body : some View {
VStack(spacing: 0) {
// Some text ...
Text("gsfdsfkajflkasdjflkas,jdflaskjf")
HStack(spacing:0) {
Button(action: {print("hi")}) { Text("Cancel") }
.padding().inExpandingRectangle().fixedSize(horizontal: true, vertical: true)
// This divider is the problem
Divider()
Button(action: {print("hello")}) { Text("Delete") }
.padding().inExpandingRectangle().fixedSize(horizontal: true, vertical: true)
}.fixedSize() <------- Insert this
}.frame(minHeight: 0)
}
结果:
这是一个可能的简化替代方案演示,没有 人工 扩展。使用 Xcode 11.4 / iOS 13.4.
测试
Divider() // or Rectangle().fill(Color.gray).frame(height: 1)
HStack {
Button(action: {}) { Text("Cancel").fixedSize() }
.padding().frame(maxWidth: .infinity)
Divider() // or Rectangle().fill(Color.gray).frame(width: 1)
Button(action: {}) { Text("Delete").fixedSize() }
.padding().frame(maxWidth: .infinity)
}.fixedSize(horizontal: false, vertical: true)
注意:还值得考虑 自定义 分隔符,例如
Rectangle().fill(Color.gray).frame(width: 1) // or any other color
可能会提供更合适的视觉反馈,例如
您可以试试下面的代码希望对您有所帮助(Xcode 11.2.1)
注意:根据您的要求更改Font
、Frame
和Color
var body: some View {
VStack(spacing: 0) {
Text("Delete")
.font(Font.system(size: 20, weight: .bold))
Text("Are you sure you want to delete ?")
.font(Font.system(size: 18, weight: .regular))
.padding([.top,.bottom], 15)
Rectangle().fill(Color.gray.opacity(0.5)).frame(height:0.5)
HStack(spacing:0) {
Button(action: {
//Your action
}) {
Text("Cancel")
.foregroundColor(Color.black)
.font(Font.system(size: 18, weight: .medium))
}
.frame(minWidth: 150, maxWidth: .infinity, minHeight: 40, maxHeight: 40, alignment: .center)
Rectangle().fill(Color.gray.opacity(0.5)).frame(width:0.5,height:20)
Button(action: {
//Your action
}) {
Text("Delete")
.font(Font.system(size: 18, weight: .medium))
.foregroundColor(Color.red)
}
.frame(minWidth: 150, maxWidth: .infinity, minHeight: 40, maxHeight: 40, alignment: .center)
}
}
}
输出:
我正在使用 SwiftUI 创建类似于警报弹出窗口的东西,我使用 UIHostingController 从 UIKit 代码中呈现它。视图如下所示:
VStack(spacing: 0) {
// Some text ...
HStack(spacing:0) {
Button(action: self.onCancel) { Text("Cancel") }
.padding().inExpandingRectangle().fixedSize(horizontal: false, vertical: true)
// This divider is the problem
Divider() // .fixedSize()
Button(action: self.onDelete) { Text("Delete") }
.padding().inExpandingRectangle().fixedSize(horizontal: false, vertical: true)
}
}.frame(minHeight: 0)
inExpandingRectangle
是我在另一个 Whosebug 问题中发现的。它将文本居中放置在 HStack 的每一侧。
extension View {
func inExpandingRectangle() -> some View {
ZStack {
Rectangle().fill(Color.clear)
self
}
}
}
看起来像这样。垃圾。
如果我将 .fixedSize()
放在分隔线上,它会执行此操作。不可怕,但分隔线看起来很愚蠢,不会扩展到按钮的大小。
在 HStack
上放置 fixedSize()
修饰符而不是 Divider
可以解决问题。
var body : some View {
VStack(spacing: 0) {
// Some text ...
Text("gsfdsfkajflkasdjflkas,jdflaskjf")
HStack(spacing:0) {
Button(action: {print("hi")}) { Text("Cancel") }
.padding().inExpandingRectangle().fixedSize(horizontal: true, vertical: true)
// This divider is the problem
Divider()
Button(action: {print("hello")}) { Text("Delete") }
.padding().inExpandingRectangle().fixedSize(horizontal: true, vertical: true)
}.fixedSize() <------- Insert this
}.frame(minHeight: 0)
}
结果:
这是一个可能的简化替代方案演示,没有 人工 扩展。使用 Xcode 11.4 / iOS 13.4.
测试Divider() // or Rectangle().fill(Color.gray).frame(height: 1)
HStack {
Button(action: {}) { Text("Cancel").fixedSize() }
.padding().frame(maxWidth: .infinity)
Divider() // or Rectangle().fill(Color.gray).frame(width: 1)
Button(action: {}) { Text("Delete").fixedSize() }
.padding().frame(maxWidth: .infinity)
}.fixedSize(horizontal: false, vertical: true)
注意:还值得考虑 自定义 分隔符,例如
Rectangle().fill(Color.gray).frame(width: 1) // or any other color
可能会提供更合适的视觉反馈,例如
您可以试试下面的代码希望对您有所帮助(Xcode 11.2.1)
注意:根据您的要求更改Font
、Frame
和Color
var body: some View {
VStack(spacing: 0) {
Text("Delete")
.font(Font.system(size: 20, weight: .bold))
Text("Are you sure you want to delete ?")
.font(Font.system(size: 18, weight: .regular))
.padding([.top,.bottom], 15)
Rectangle().fill(Color.gray.opacity(0.5)).frame(height:0.5)
HStack(spacing:0) {
Button(action: {
//Your action
}) {
Text("Cancel")
.foregroundColor(Color.black)
.font(Font.system(size: 18, weight: .medium))
}
.frame(minWidth: 150, maxWidth: .infinity, minHeight: 40, maxHeight: 40, alignment: .center)
Rectangle().fill(Color.gray.opacity(0.5)).frame(width:0.5,height:20)
Button(action: {
//Your action
}) {
Text("Delete")
.font(Font.system(size: 18, weight: .medium))
.foregroundColor(Color.red)
}
.frame(minWidth: 150, maxWidth: .infinity, minHeight: 40, maxHeight: 40, alignment: .center)
}
}
}
输出: