swift 是否可以将覆盖命令作为if之后的下一条语句?[duplicate]

ecfdbz9o  于 2022-12-03  发布在  Swift
关注(0)|答案(1)|浏览(76)

This question already has an answer here:

Overlay views one by one in SwiftUI (1 answer)
Closed 2 days ago.
Is it possible to use an if before an overlay?
The example code shows a blue square with an overlay of text and a highlight just below the text. And if the number in the text matches the day number of the month, then the blue box shows a white border.
Getting the code to work is fairly easy if you only have one if statement, because you can place all objects inside the if. Although if there are multiple if scenarios (such as different border colours according to different situations) the most efficient way (from the perspective of making it readable) would be to insert the if between the Color object and the .overlay.
As a result of trying to make it more efficient I have tried a variety of methods (such as self.overlay etc).
This is the example and shortened code - I have inserted the if statement after the "//" in lines 7 to ten that I think would make it more efficient (to read) if I could get it to work (where I would put a series of ifs):

struct exampleCode: View {
        let textRef : Int
        var body : some View {
                Color(red:20/255,green: 45/255, blue:50/255)
                    .frame(width:50, height:56)
                    .cornerRadius(6)
                //if(Calendar.current.component(.day, from: Date()) == dayRef){
                //    self.overlay(RoundedRectangle(cornerRadius: 5)
                //        .stroke(Color.white, lineWidth: 2))
                //}
                    .overlay(alignment:.center){
                VStack (spacing: -4){
                Spacer()
                Text(String(textRef))
                    .foregroundColor(.white)
                    .font(Font.system(size: 26, weight: .bold))
                Spacer()
                    Color(red:20/255,green: 255/255, blue:71/255)
                        .frame(width:20, height:6)
                }
            }
        }
    }
7d7tgy0s

7d7tgy0s1#

不,这不可能。
一种解决方案是条件ViewModifier,但这会创建一个新视图,可能会中断动画。
还有一个替代方案:应用三元运算符并传递nil以表示 no change

struct ExampleCode: View { // please name struct uppercase
    let textRef : Int
    let isSameDay = Calendar.current.component(.day, from: Date()) == dayRef // where does dayRef come from?
    var body : some View {
            Color(red:20/255,green: 45/255, blue:50/255)
                .frame(width:50, height:56)
                .cornerRadius(6)
                .overlay(isSameDay 
                    ? RoundedRectangle(cornerRadius: 5)
                        .stroke(Color.white, lineWidth: 2)
                    : nil)

                .overlay(alignment:.center){
            VStack (spacing: -4){
            Spacer()
            Text(String(textRef))
                .foregroundColor(.white)
                .font(Font.system(size: 26, weight: .bold))
            Spacer()
                Color(red:20/255,green: 255/255, blue:71/255)
                    .frame(width:20, height:6)
            }
        }
    }
}

相关问题