美文网首页
关于使用swiftUI开发时关于UI基类的想法

关于使用swiftUI开发时关于UI基类的想法

作者: Fillinse | 来源:发表于2023-10-19 10:10 被阅读0次

先说明,因为菜,所以不接受吐槽。
最近因为一些需求,所以会把一些swift代码写的界面修改成swiftUI,因为之前UIViewController可以继承,所以能做一些公共操作。但是工程目前所有界面都是隐藏了导航栏,并且使用的自定义的导航栏,而且会有一些界面间的公共主题设置。所以就考虑做一个UI基类。但是,View并不支持继承,所以最后就想了一个不是办法的办法,效果如下:


截屏2023-10-19 18.12.59.png

使用还是方便,可以根据自己的需要进行自定义的封装。
代码如下:

struct AAFBaseContainnerViewSwiftUI<Content> : View where Content : View {
   typealias BodyContent = () -> Content

   var body: Never

   static func container(hideBar:Bool = false,title: String,leftHide: Bool = false,right: String = "",content: BodyContent?) -> some View {
      return GeometryReader(content: { geometry in
         VStack{
            if hideBar == false {
               VStack{
                  Rectangle().frame(height:statusHeight())
                     .foregroundColor(Color.clear)
                  HStack{
                     HStack {
                        if leftHide == false {
                           Image("icon_ty_fh")
                              .frame(width: 44,height: 44,alignment: .center)
                        } else {
                           Rectangle().foregroundColor(Color.clear)
                              .clipped()
                              .frame(width: 44,height: 44,alignment: .center)
                        }
                        Spacer()
                     }
                     Spacer()
                     HStack {
                        Text(title)
                           .font(17.medium)
                     }
                     Spacer()
                     HStack {
                        Spacer()
                        Text(right)
                           .font(15.regulare)
                           .padding(.trailing,12)
                           .frame(minWidth: 44)
                           .foregroundColor("#FEDB7F".c)
                     }
                  }
                  .foregroundColor(Color.white)
               }
               .frame(width: geometry.size.width,height: naviBarHeight())
            }
            if content != nil {
               content!()
            } else {
               Rectangle().foregroundColor(.clear)
            }
         }
         .frame(width: geometry.size.width,height: geometry.size.height,alignment: .top)
         .background("#5774B7".c)
         .edgesIgnoringSafeArea(.vertical)
      })
      .edgesIgnoringSafeArea(.all)
   }
}
#Preview {
   AAFBaseContainnerViewSwiftUI.container(title: "222",leftHide: false,right: "右边") {
      ///每个界面的各自内容
      VStack(alignment: .center, spacing: 0, content: {
         Text("界面的内容")
            .foregroundColor(.white)
      })
   }
}

2.0 viewModifier版,写了几个界面感觉使用不是很顺畅,参考了之前看的斯坦福教程的demo,将其修改为2.0版

struct BaseViewModifier: ViewModifier {
   @Environment(\.presentationMode) var presentationMode
   var hideBar: Bool
   var title: String
   var leftHide: Bool
   var right: String
   var hideLine: Bool
   var leftAction: ActionBlock?
   var rightAction: ActionBlock?

   // 隐藏导航
   init() {
      self.title = ""
      self.hideBar = true
      self.leftHide = false
      self.right = ""
      self.hideLine = false
   }

   // 常规情况,有左返回,有标题
   init(title: String) {
      self.title = title
      self.hideBar = false
      self.leftHide = false
      self.right = ""
      self.hideLine = false
   }

   // 登录页专用
   init(isCheck: Bool, leftAction: ActionBlock?, rightAction: ActionBlock?) {
      self.title = ""
      self.hideBar = false
      if isCheck {
         self.leftHide = false
         self.right = "去注册"
      } else {
         self.leftHide = true
         self.right = ""
      }
      self.hideLine = true
      self.leftAction = leftAction
   }

   func body(content: Content) -> some View {
      GeometryReader(content: { geometry in
         VStack {
            if hideBar != true {
               VStack {
                  Rectangle().frame(height: statusHeight())
                     .foregroundColor(Color.clear)
                  HStack {
                     HStack {
                        if leftHide == false {
                           Image("icon_ty_fh")
                              .frame(width: 44, height: 44, alignment: .center)
                              .onTapGesture {
                                 if let block = leftAction {
                                    block()
                                 } else {
                                    presentationMode.wrappedValue.dismiss()
                                 }
                              }
                        } else {
                           Rectangle().foregroundColor(Color.clear)
                              .clipped()
                              .frame(width: 44, height: 44, alignment: .center)
                        }
                        Spacer()
                     }
                     Spacer()
                     HStack {
                        Text(title)
                           .font(17.medium)
                     }
                     Spacer()
                     HStack {
                        Spacer()
                        Text(right)
                           .font(15.regulare)
                           .padding(.trailing, 12)
                           .frame(minWidth: 44)
                           .foregroundColor("#FEDB7F".c)
                           .onTapGesture {
                              if let block = rightAction {
                                 block()
                              }
                           }
                     }
                  }
                  .frame(height: naviBarHeight() - statusHeight() - 1)
                  .foregroundColor(Color.white)
                  Rectangle().frame(width: kScreenWidth, height: 1).foregroundColor("#6478D1".c.a(hideLine ? 0 : 0.4))
               }
               .frame(width: geometry.size.width, height: naviBarHeight())
            }
            content
               .padding(.bottom, bottomSafeHeight())
               .navigationBarHidden(true)
               .navigationViewStyle(.stack)
         }
         .frame(width: geometry.size.width, height: geometry.size.height, alignment: .top)
         .background(LinearGradient(colors: ["#384891".c, "#5E7EBF".c], startPoint: .top, endPoint: .bottom))
      })
      .edgesIgnoringSafeArea(.all)
   }
}

extension View {
   // 构建常规 左返回 + 标题
   func baseNormal(title: String) -> some View {
      self.modifier(BaseViewModifier(title: title))
   }
   // 隐藏导航
   func baseNone() -> some View {
      self.modifier(BaseViewModifier())
   }
   // 登录页专用
   func baseLogin(isCheck: Bool,leftAction: ActionBlock?, rightAction: ActionBlock?) -> some View {
      self.modifier(BaseViewModifier(isCheck: isCheck, leftAction: leftAction, rightAction: rightAction))
   }
}

调用更加方便顺滑,侵入性更低,只需在最外层的界面调用即可

         .baseNormal(title: "忘记密码")

相关文章

网友评论

      本文标题:关于使用swiftUI开发时关于UI基类的想法

      本文链接:https://www.haomeiwen.com/subject/qoecidtx.html