I am trying to create a custom alert where I can add progress bar/image (to show error or successful) message. I came across this library but it doesn’t support iOS 13.
For inbuilt alert API, it’s not configurable.
.alert(isPresented: $showMessage, content: {
Alert(
title: Text("Info"),
message: Text(alertMessage),
dismissButton: .default(Text("Ok"))
)
})
My custom alert:
struct CustomAlert: View {
@Environment(.presentationMode) var presentationMode
let connectionStatus:Bool
let iconName:String
let title :String
let discription:String
var agreeHandler:() -> Void
var disagreeHandler:() -> Void
let screenWidth = UIScreen.main.bounds.width
var body: some View {
VStack(spacing: 0){
HStack (spacing: 0){
Spacer()
Text("xmark")
.font(.custom(Fonts.FontAwesome6ProRegular, size: screenWidth * 0.08))
.foregroundColor(Color(ViewControllerCosntants.secondaryColor200))
.onTapGesture {
disagreeHandler()
}
}
.padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
Text(iconName)
.font(Font.custom(Fonts.FontAwesome6ProSolid, size: screenWidth * 0.341))
.padding(.top, 50)
.foregroundColor(Color("Primary600"))
Text(title)
.tracking(0.24)
.font(.custom(Fonts.RobotoRegular, size: screenWidth * 0.085 ))
.padding(EdgeInsets(top: 35, leading: 35, bottom: 7, trailing: 35))
VStack(alignment: .leading){
HStack{
Text("Currently:")
.tracking(0.18)
.font(.custom(Fonts.RobotoRegular, size: screenWidth * 0.064 ))
Text(connectionStatus ? "Enabled" : "Disabled")
.tracking(0.18)
.font(.custom(Fonts.RobotoRegular, size: screenWidth * 0.064 ))
.foregroundColor( connectionStatus ? .green : .red)
}
}
Text(discription)
.tracking(0.12)
.font(.custom(Fonts.RobotoRegular, size: 0.042 * screenWidth))
.padding(EdgeInsets(top: 30, leading: 25, bottom: 72, trailing: 25))
.foregroundColor(Color("Secondary700"))
.lineSpacing(10)
HStack(spacing: 0){
Text("DISAGREE")
.tracking(1.15)
.font(.custom(Fonts.RobotoRegular, size: 0.048 * screenWidth))
.foregroundColor(Color(ViewControllerCosntants.primayColor))
.onTapGesture {
disagreeHandler()
}
Spacer()
Text("AGREE")
.tracking(1.15)
.font(.custom(Fonts.RobotoRegular, size: 0.048 * screenWidth))
.foregroundColor(Color(ViewControllerCosntants.primayColor))
.onTapGesture {
agreeHandler()
}
}
.padding(EdgeInsets(top: 0, leading: 25, bottom: 20, trailing: 25))
}
.frame(width: UIScreen.main.bounds.width-50)
.background(Color.white)
.cornerRadius(15)
.shadow(radius: 25)
}
}
Is there any way I can create a single alert that I can reuse? Currently I am using separate views and showing as alert but I am sure there must be some cleaner way to do that.