Posts

Post marked as solved
2 Replies
801 Views
Hi! I'm trying to create an extension to reduce work when use View Code. I've just been in a lab but they couldn't had enough time to fully solve my problem. So, before the lab I had: extension UIView { func fix(in otherView: UIView, to side: ConstraintType, with	constant: CGFloat) -> NSLayoutConstraint {         switch side {         case .top:             return self.topAnchor.constraint(equalTo: otherView.topAnchor, constant: constant)         case .bottom:             return self.bottomAnchor.constraint(equalTo: otherView.bottomAnchor, constant: constant)         case .left:             return self.leftAnchor.constraint(equalTo: otherView.leftAnchor, constant: constant)         case .right:             return self.rightAnchor.constraint(equalTo: otherView.rightAnchor, constant: constant)         case .centerX:             return self.centerXAnchor.constraint(equalTo: otherView.centerXAnchor, constant: constant)         case .centerY:             return self.centerYAnchor.constraint(equalTo: otherView.centerYAnchor, constant: constant)         default: return NSLayoutConstraint()         }     } } public enum ConstraintType {     case top, bottom, left, right, centerX, centerY } It works, but it's too big and, in this case, I only have one side (for both views). If I added the side of the other view, this will be huge.. After the lab, I got this: extension UIView {     func fix(in otherView: UIView, to side: ConstraintType, with constant: CGFloat) -> NSLayoutConstraint {         return side.getAnchor(view: self).constraint(equalTo: side.getAnchor(view: otherView))     }      } protocol ConstraintType {     func getAnchor(view: UIView) -> Anchor     associatedtype Anchor } public enum ConstraintYType: ConstraintType {     typealias Anchor = NSLayoutYAxisAnchor     case bottom, top, centerY     func getAnchor(view: UIView) -> Anchor {         switch self {         case .bottom:             return view.centerYAnchor         case .top:             return view.topAnchor         case .centerY:             return view.centerYAnchor         }     } } public enum ConstraintXType: ConstraintType {     typealias Anchor = NSLayoutXAxisAnchor     case left, right, centerX     func getAnchor(view: UIView) -> Anchor {         switch self {         case .left:             return view.leftAnchor         case .right:             return view.rightAnchor         case .centerX:             return view.centerXAnchor         }     } } It's really better! But, I'm getting an error: Protocol 'ConstraintType' can only be used as a generic constraint because it has Self or associated type requirements and... Member 'getAnchor' cannot be used on value of protocol type 'ConstraintType'; use a generic constraint instead So, now my question is: how can I fix it or do it in other way?
Posted Last updated
.
Post not yet marked as solved
0 Replies
547 Views
Hi everyone! I have a custom form that is a list with shadow and in below a button. Because of this design, I had to create a List inside a ScrollView. However, I notice that my list have no height after it. Then, I need to make explicit what the "expected" size of my List. My question is: how do I use it with the automatic size (calculated using the cells' size)? The code: struct SwiftUIView: View {     var body: some View {         ZStack {             ScrollView(.vertical, showsIndicators: false) {                 Spacer()                     .frame(height: 20)                 List {                     ForEach(1...6, id: \.self) { index in                         VStack(alignment: .trailing, spacing: 5) {                             Spacer()                                 .frame(height: 5)                             HStack {                                 Text("title")                                     .foregroundColor(Color.gray)                                 Spacer()                             }                             HStack {                                 Text("subtitle")                                 Spacer(minLength: 20)                                 Image(systemName: "pencil")                             }                                                          Spacer()                                 .frame(height: 2)                             Rectangle()                                 .fill(Color.gray)                                 .frame(width: UIScreen.screenWidth - 50, height: 0.7)                         }                                              }                 }                 .shadow(color: Color.black, radius: 4)                 .frame(width: UIScreen.screenWidth - 20, height: 500)                 .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))                 Spacer()                     .frame(height: 20)                 Button(action: {                     //do the request                 }, label: {                     Text("Request")                         .foregroundColor(Color.white)                         .frame(width: UIScreen.screenWidth - 60, height: 50)                         .background(Color.red)                         .cornerRadius(33)                 })             }         }     } } struct SwiftUIView_Previews: PreviewProvider {     static var previews: some View {         SwiftUIView()     } }
Posted Last updated
.
Post marked as solved
1 Replies
287 Views
Hi! I have a custom struct that have 3 optional generic views (so I can use as Text, Navigation Item, Button or anything):struct CustomView<FirstT: View, SecondT: View, ThirdT: View>: View { var view1: FirstT? var view2: SecondT? var view3: ThirdT? var body: some View { ... } } So, my the problem is when I am going to use it. Swift can't infer the type of the views, even when the are nil. How can I solve this problem? I need this to use my init like this:CustomView(view1: Text("Hi!")) Currently I'm using like this: CustomView(view1: Text("Hi!"), view2: Text(""), view3: : Text("")) or CustomView<Text, Text, Text>(view1: Text("Hi!"))
Posted Last updated
.