简体   繁体   中英

How to change background color and image when it's tapped to the button in swift UI?

I want to change background color and add an okay button image to the selection point when I tap to that point. How can I do that? I do know how to change the background color but I'm not sure how can I add confirmation image in. Should I use ZStack for this or is there any other way to do so?

在此处输入图像描述

在此处输入图像描述

HStack {
                            Button(action: {
                                self.tap1.toggle()
                                
                                
                            }) {
                                Text("")
                                .padding(.horizontal, 10)
                                .padding(.vertical, 5)
                                .background((self.tap1) ? Color(.gray) : Color(.blue)
                                .cornerRadius(4)
                                .padding(.leading, 40)
                            }
                            
                            Spacer()
                            
                            Text("Diyabet")
                                .font(.system(size: 20, weight: .regular, design: .rounded))
                                .padding(.trailing, 200)
                                Spacer()
                        }.padding(.bottom, 20)
                        
                        
                        HStack {
                            Button(action: {
                                self.tap2.toggle()
                               
                                
                            }) {
                               Text("")
                                .padding(.horizontal, 10)
                                .padding(.vertical, 5)
                                .background(self.tap2  ? Color(.gray) : Color(.blue)
                                .cornerRadius(4)
                                .padding(.leading, 40)
                            }
                            
                            Spacer()
                            
                            Text("Yüksek Tansiyon")
                                .font(.system(size: 20, weight: .regular, design: .rounded))
                                .padding(.trailing, 130)
                            Spacer()
                        }.padding(.bottom, 20)
                    
                        HStack {
                            Button(action: {
                                self.tap3.toggle()
                                
                            }) {
                                Text("")
                                .padding(.horizontal, 10)
                                .padding(.vertical, 5)
                                .background(self.tap3  ? Color(.gray) : Color(.blue)
                                .cornerRadius(4)
                                .padding(.leading, 40)
                            }
                            
                            Spacer()
                            
                            Text("Kalp ve Damar Hastalıkları")
                                .font(.system(size: 20, weight: .regular, design: .rounded))
                                .padding(.trailing, 45)
                            Spacer()
                        }.padding(.bottom, 20)
@State private var pressed = false

var body: some View {
    ZStack {
        Button(action: {
            self.pressed.toggle()
        }) {
            ZStack {
                Rectangle().fill(self.pressed ? Color.blue : Color.gray)
                if self.pressed {
                    Image(systemName: "checkmark")
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM