【问题标题】:How to apply modifier or view by condition如何应用修饰符或按条件查看
【发布时间】:2019-11-25 10:18:29
【问题描述】:
@State var modifierEnabled : Bool

struct BlankModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
    }
}

extension View {
    func TestModifierView() -> some View{
       return self.modifier(BlankModifier())
    }
}

只有modifierEnabled == true的情况下如何申请TestModifierView

【问题讨论】:

    标签: swiftui


    【解决方案1】:
    @available(OSX 11.0, *)
    public extension View {
        @ViewBuilder
        func `if`<Content: View>(_ condition: Bool, content: (Self) -> Content) -> some View {
            if condition {
                content(self)
            } else {
                self
            }
        }
    }
    
    @available(OSX 11.0, *)
    public extension View {
        @ViewBuilder
        func `if`<TrueContent: View, FalseContent: View>(_ condition: Bool, ifTrue trueContent: (Self) -> TrueContent, else falseContent: (Self) -> FalseContent) -> some View {
            if condition {
                trueContent(self)
            } else {
                falseContent(self)
            }
        }
    }
    

    用法示例(一个修饰符):

    Text("some Text")
       .if(modifierEnabled) { $0.foregroundColor(.Red) }
    

    用法示例2(与条件相关的两个修饰符链):

    Text("some Text")
       .if(modifierEnabled) { $0.foregroundColor(.red) } 
       else:                { $0.foregroundColor(.blue).background(Color.green) }
    

    但是!!!!!!!!!!!!

    重要的是,此修饰符可能是某些缩进问题的原因。 (稍后你会明白这一点)

    所以在某些情况下最好使用标准的if 构造

    【讨论】:

      【解决方案2】:

      我喜欢没有类型橡皮擦的解决方案。它看起来严谨而优雅。

      public extension View {
          @ViewBuilder
          func modify<TrueContent: View, FalseContent: View>(_ condition: Bool, ifTrue modificationForTrue: (Self) -> TrueContent, ifFalse modificationForFalse: (Self) -> FalseContent) -> some View {
              if condition {
                  modificationForTrue(self)
              } else {
                  modificationForFalse(self)
              }
          }
      }
      

      用法

      HStack {
      ...
                  }
                  .modify(modifierEnabled) { v in
                          v.font(.title)
                      } ifFalse: {
                          $0.background(Color.red) // even shorter
                      }
      

      如果您只打算应用修饰符(或修饰符链),请考虑:

      @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
      public extension View {
          @ViewBuilder func modifier<VM1: ViewModifier, VM2: ViewModifier>(_ condition: @autoclosure () -> Bool, applyIfTrue: VM1, applyIfFalse: VM2
      ) -> some View {
              if condition() {
                 self.modifier(applyIfTrue)
              } else {
                 self.modifier(applyIfFalse)
              }
          }
      }
      

      使用几乎和普通的.modifier 一样简单。

      ...

          Form {
              HStack {
      ...
                  }
                  .modifier(modifierEnabled, applyIfTrue: CornerRotateModifier(amount: 8, anchor: .bottomLeading), applyIfFalse: EmptyModifier())
      

      ...

      为简洁起见,您可以省略applyIfFalse 部分,如果条件为假,则可以省略return self.erase()

      【讨论】:

        猜你喜欢
        • 2022-05-28
        • 1970-01-01
        • 2017-03-13
        • 2021-11-18
        • 2012-02-05
        • 2022-06-16
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多