应用修饰符于 if else 块

huangapple go评论54阅读模式
英文:

Apply modifier on if else block

问题

如下所示,我已经在 if 块和 else 块中应用了 frame() 修饰符。

var body: some View {
    NavigationView {
        if model.mode == .active {
            activeList
                .frame(minWidth: 200)
        } else {
            trashedList
                .frame(minWidth: 200)
        }
    }
}

如何去除 frame(minWidth:) 的重复?

更一般地说,是否有一种方法可以在 if else 结果中将修饰符放在一个公共位置?

英文:

As seen in the code below, I have applied frame() modifier in the if block and else block.

var body: some View {
    NavigationView {
        if model.mode == .active {
            activeList
                .frame(minWidth: 200)
        } else {
            trashedList
                .frame(minWidth: 200)
        }
    }
}

How do I remove duplication of frame(minWidth:)?

More generally, is there a way where outcome of if else can have modifiers at a common place?

答案1

得分: 2

虽然以上所有答案都是正确的,但我的个人观点是像下面这样做。我更喜欢 @paul 的观点,使用一个组可以减少你的视图修饰符的重复。

var body: some View {
    NavigationView {
        let listView = model.mode == .active ? activeList : trashedList
        
        listView
            .frame(minWidth: 200)
    }
}

希望对你有帮助!

英文:

Though all of the above answers are correct, my personal opinion is to do something like below. And i prefer the @paul's view having a group can reduce your view modifier's replication.

var body: some View {
    NavigationView {
        let listView = model.mode == .active ? activeList : trashedList
        
        listView
            .frame(minWidth: 200)
    }
}

Hope it helps!

答案2

得分: 1

我建议您使用Group,因为它不会更改您的布局,如下所示:

var body: some View {
    NavigationView {
        Group {
            if model.mode == .active {
                activeList
            } else {
                trashedList
            }
        }
        .frame(minWidth: 200)
    }
}
英文:

I can advice you to use Group as it doesn't change your layout such as :

var body: some View {
    NavigationView {
        Group {
            if model.mode == .active {
                activeList
            } else {
                trashedList
            }
        }
        .frame(minWidth: 200)
    }
}

答案3

得分: 0

以下是翻译好的部分:

这是如何定义自己的修饰器的方法:

struct MyDefaultWidthModifier: ViewModifier {    
    func body(content: Content) -> some View {
        return content
            .frame(minWidth: 200) // 根据您的需求,您可以将这个值存储为变量
    }
}

优雅的用法:

NavigationView {
        if model.mode == .active {
            activeList
                .modifier(MyDefaultWidthModifier())
        } else {
            trashedList
                .modifier(MyDefaultWidthModifier())
        }
    }

解决方案2:如果您不想每次都编写修饰器,请扩展ListStyle。以下是示例:

struct MyListStyle: ListStyle {
    func makeBody(configuration: Configuration) -> some View {
        // 这只是扩展了默认的List外观,但您可以返回

        List(configuration)
            .modifier(MyDefaultWidthModifier()) // 以及其他样式
    }
}

extension ListStyle where Self == MyListStyle {
    static var myListStyle: MyListStyle { .init() }
}

然后在您的顶级ContentView中添加修饰器:

var body: some View {
    NavigationView {
        if model.mode == .active {
            activeList
        } else {
            trashedList
        }
    }.listStyle(.myListStyle)
}
英文:

Here is how you can define your own modifier

struct MyDefaultWidthModifier: ViewModifier {    
    func body(content: Content) -> some View {
        return content
            .frame(minWidth: 200) // you can store this as a variable based on your needs
    }
}

Elegant Usage:

NavigationView {
        if model.mode == .active {
            activeList
                .modifier(MyDefaultWidthModifier())
        } else {
            trashedList
                .modifier(MyDefaultWidthModifier())
        }
    }

Solution 2: If you don't want to write the modifier every time, extend the ListStyle. Example below:

struct MyListStyle: ListStyle {
    func makeBody(configuration: Configuration) -> some View {
        // This just extends the default List appearance, but you can return

        List(configuration)
            .modifier(MyDefaultWidthModifier()) // Along with whatever other styles you 
    }
}

extension ListStyle where Self == MyListStyle {
    static var myListStyle: MyListStyle { .init() }
}

Then in your top-level ContentView, add the modifier:

var body: some View {
    NavigationView {
        if model.mode == .active {
            activeList
        } else {
            trashedList
        }
    }.listStyle(.myListStyle)
}

huangapple
  • 本文由 发表于 2023年6月29日 10:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定