divadretlaw / customalert Goto Github PK
View Code? Open in Web Editor NEW🔔 Custom Alert for SwiftUI
License: MIT License
🔔 Custom Alert for SwiftUI
License: MIT License
Hi i'm trying to build an app on a pipeline and is giving me this error
Signing for "CustomAlert_CustomAlert" requires a development team. Select a development team in the Signing & Capabilities editor.
and i think id in the demo section of the library.
how can i change the team id on the library?
When adding a text field on the alert (both body and actions) the field cannot be selected. Am I missing something?
BTW, amazing project!
I used the demo to show the issue, alert shows empty text, only second opening has correct value. For now i delay showing the alert so gets new value but seems a bit hacky, any solution? The native Alert() has no issue.
struct SimpleAlerts: View {
@State private var showNative = false
@State private var showCustom = false
@State var text = ""
var body: some View {
Section {
Button {
showNative = true
} label: {
Text("Native Alert")
}
.alert("Native Alert", isPresented: $showNative) {
Button(role: .cancel) {
} label: {
Text("Cancel")
}
} message: {
Text("Some Message")
}
Button {
showCustom = true
text = "text"
} label: {
Text("Custom Alert")
}
.customAlert("Custom Alert", isPresented: $showCustom) {
Text(text)
} actions: {
Button(role: .cancel) {
} label: {
Text("Cancel")
}
}
} header: {
Text("Simple")
}
}
}
.simultaneousGesture(TapGesture().onEnded { _ in guard isEnabled else { return } isPresented = false })
this will disable button action
Hi David (or anyone contributing to this repo),
Since upgrading to Xcode 15 I have noticed an issue, but couldn't figure out the underlying cause. I wonder if you experienced anything similar?
Sample alert snippet:
.customAlert(isPresented: $isInsideCommenting) {
// MARK: Inside Commenting on Action
Text("Action Comment")
.font(.subheadline)
.padding(.bottom, 4)
getMultilineTextField("Comment for \(viewModel.action.name ?? "")", $insideComment)
} actions: {
/// Inside Commenting Popup Buttons
MultiButton {
Button("Cancel", role: .cancel) {
insideComment = viewModel.comment
isInsideCommenting = false
}
Button("OK") {
let sanitisedComment = insideComment.trimmingCharacters(in: .whitespacesAndNewlines)
if sanitisedComment.isEmpty {
viewModel.deleteComment()
} else {
viewModel.addComment(insideComment)
}
commentRecorded?()
isInsideCommenting = false
}
}
}
Sample multiline text field constructor method:
@ViewBuilder
func getMultilineTextField(_ title: String,
_ stringValue: Binding<String>) -> some View {
if #available(iOS 16.0, *) {
TextField(title, text: stringValue, axis: .vertical)
.lineLimit(5, reservesSpace: true)
.textFieldStyle(.roundedBorder)
.multilineTextAlignment(.leading)
.accessibilityLabel("TextFieldInAlert")
} else {
// Fallback on earlier versions
TextField(text: stringValue) {
Text(title).multilineTextAlignment(.leading)
}.accessibilityLabel("TextFieldInAlert")
}
}
Thanks for the effort building a custom alert, much appreciated.
There is an error when you have an EnvironmentObject in the parent view; when you activate the alert, the parent view loses access to the EnvironmentObject.
Cheers
Hi, thank you for the great framework. I run into some issues where the alert buttons didn't get updated. Here's the code:
.customAlert("Enter access code",
isPresented: $isShowingAccessCodeAlert,
content: {
TextField("Access Code", text: $accessCode)
.onSubmit {
submitCode()
isShowingAccessCodeAlert = false
}
}, actions: {
MultiButton {
Button(role: .cancel) {
} label: {
Text("Cancel")
}
Button("Submit", action: submitCode)
.disabled(accessCode.trimmingCharacters(in: .whitespacesAndNewlines).count == 0)
.bold()
}
})
it doesn't seem like the button disable state is getting updated when the user starting to enter text. What did I do wrong? Thanks!
Currently I need to do something like the following to get the localized text for the alert:
extension String {
func getLocalizedTextForAlert() {
let path = Bundle.main.path(forResource: "ar", ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
}
where ar
is one of the supported languages in my app (Arabic), because doing just: Text("some text") will always fallback to the English version of the some text
text. Giving that I have two Localizable.strings
files in the app: one for en
and the other for ar
localization.
In the current swiftui sheet that has a modifier that take a binding item as parameter and shows the sheet when the parameter is not nil. It would be very helpful if there is a way to use similar behavior on this library. BTW Thanks a lot for this awesome and handly library
For example:
.sheet(item: $deleteConfirmation, content: { item in })
I noticed that in the latest release I have to press the buttons really long to get an effect.
If I press them normally, no button callback is triggered, the alert just disappears.
I believe it was introduced by 3.5.0
.
3.3.0
I am unable to install (does not resolve in Xcode).
On 3.1.0
I am unable to reproduce.
My code to show an alert:
.customAlert(confirmationSubtitle, isPresented: $showSelectionConfirmaionAlert) {
// view code with labels and image
} actions: {
Button("test 1") {
// only called when long-pressing button
}
Button("test 2") {
// only called when long-pressing button
}
}
I use customAlert view modifier like that:
.customAlert("Some Fancy Alert", isPresented: $restoring) {
Text("I'm a custom Message")
.font(.custom("Noteworthy", size: 24))ProgressView() .progressViewStyle(.circular) } actions: { Button(role: .cancel) { // some Action } label: { Text("Cancel") }
}
Add the result is just like attachment.
Use SwiftUI API can auto alert when page first loaded, but your code cannot.
SwiftUI API code:
@State private var showAlert = true
VStack(spacing: 0) {
Text("首次启动弹出弹窗")
}
.alert("title", isPresented: $showAlert, actions: {
Button("取消", role: .cancel, action: {})
}, message: {
Text("Message")
})
Your code:
@State private var showAlert = true
VStack(spacing: 0) {
Text("首次启动弹出弹窗")
}
.customAlert("Some Fancy Alert", isPresented: $showAlert) {
Text("I'm a custom Message")
.font(.custom("Noteworthy", size: 24))
Image(systemName: "swift")
.resizable()
.scaledToFit()
.frame(maxHeight: 100)
.foregroundColor(.blue)
} actions: {
Button {
// some Action
} label: {
Label("Swift", systemImage: "swift")
}
Button(role: .cancel) {
// some Action
} label: {
Text("Cancel")
}
}
When I trigger the alert, It runs 3 times every time. When I click an action button, it is supposed to display an error message. I have to click the action button 2 times before it shows and every time I click that button it runs the alert 2 times instead of 3. I would like to know If I'm doing something wrong.
Are you thinking of a backward overlay removal option feature in the future ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.