在SwiftUI中使用fixedSIze
我做了一个视图扩展,使fixedSize更灵活 给你 它工作得很好,但我不确定是否有更简单的方法来实现这个在SwiftUI中使用fixedSIze,swiftui,Swiftui,我做了一个视图扩展,使fixedSize更灵活 给你 它工作得很好,但我不确定是否有更简单的方法来实现这个 @available(iOS 13.0, *) struct FixedSizeView<Content> : View where Content : View { var content: Content var on: Bool public init(_ on: Bool, @ViewBuilder content: () -> Conte
@available(iOS 13.0, *)
struct FixedSizeView<Content> : View where Content : View {
var content: Content
var on: Bool
public init(_ on: Bool, @ViewBuilder content: () -> Content) {
self.content = content()
self.on = on
}
var body : some View {
Group {
if on {
content.fixedSize()
} else {
content
}
}
}
}
@available(iOS 13.0, *)
extension View {
func fixedSize(active: Bool) -> FixedSizeView<Self> {
FixedSizeView(active) {
self
}
}
}
@可用(iOS 13.0,*)
结构FixedSizeView:视图,其中内容:视图{
var-content:content
var on:Bool
public init(uon:Bool,@ViewBuilder content:()->content){
self.content=content()
self.on=on
}
var body:一些观点{
团体{
如果打开{
content.fixedSize()
}否则{
内容
}
}
}
}
@可用(iOS 13.0,*)
扩展视图{
func fixedSize(活动:Bool)->FixedSizeView{
FixedSizeView(活动){
自己
}
}
}
为什么不简化它,就像这样
extension View {
func fixedSize(active: Bool) -> some View {
Group {
if active {
self.fixedSize()
} else {
self
}
}
}
}
使用Xcode 11.2/iOS 13.2测试并运行我用这种方式尝试过,但太傻了,无法让它运行,所以我采用了复杂的方式……)这就是我问的原因。非常感谢。