Is there an solution on iOS where I could create an phone number picker menu like this? Does Apple support this view automatically or should I implement this alone?
This is called UIAlertController's ActionSheet style in iOS. You can show ActionSheet with below code:
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "Phone", style: .default) { (action) in
// Add your code here when Phone action Tapped
}
let action2 = UIAlertAction(title: "Mobile", style: .default) { (action) in
// Add your code here when Mobile action Tapped
}
let action3 = UIAlertAction(title: "iPhone", style: .default) { (action) in
// Add your code here when iPhone action Tapped
}
let action4 = UIAlertAction(title: "Private", style: .default) { (action) in
// Add your code here when Private action Tapped
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// Add your code here when Cancel action Tapped
}
// To set image in ActionButton
action1.setValue(#imageLiteral(resourceName: "check").withRenderingMode(.alwaysOriginal), forKey: "image")
// Add Actions to AlertController
actionSheet.addAction(action1)
actionSheet.addAction(action2)
actionSheet.addAction(action3)
actionSheet.addAction(action4)
actionSheet.addAction(cancel)
// Present UIAlertController
present(actionSheet, animated: true, completion: nil)
You can create your own menu as per your requirement using UIActionSheet
let actionSheet = UIActionSheet(title: "Choose Option", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Save", "Delete")
actionSheet.showInView(self.view)
Related
I don't want to change UILabel.appearance as it will apply to all labels.
How I can show UIAlertController that will look like below image?
Need to show bold button in the second position.
By default, when I am setting UIAlertActionStyle.cancel it shows it on the Confirm button in the first position.
It looks like this now:
I did some research but I didn't find any solution.
Swift 4.2/Swift 5
You need to set preferred action method,
//Create alertController
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
//Create and add the Confirm action
let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in
//Do Something here...
})
alert.addAction(confirmAction)
//Create and add the Cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
//Do Something here...
})
alert.addAction(cancelAction)
// Set Preferred Action method
alert.preferredAction = confirmAction
self.present(alert, animated: true, completion: nil)
The output will be,
You need to add them to the alertView in the right order! If you want the order to change just add the confirm button second!
alert.addAction(cancelAction)
alert.addAction(confirmAction)
Instead of:
alert.addAction(confirmAction)
alert.addAction(cancelAction)
Sure, you can. Try this
let alertView = UIAlertController(
title: "Home",
message: "Are you sure for delete Reminder",
preferredStyle: .alert)
alertView.view.tintColor = .green
let confirmAction = UIAlertAction(title: "Confirm", style: .cancel) { (_) in
// Do confirm
}
alertView.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (_) in
// Do cancel
}
alertView.addAction(cancelAction)
self.present(alertView, animated: true, completion: nil)
I have a UIAlertController and I am trying to add an arrow to it and change the sourceView to self.button so it does not appear at the bottom.
This is what I got so far:
let dropdown = UIAlertController(title: "Select Room", message: nil, preferredStyle: .ActionSheet)
let kitchen = UIAlertAction(title: "Kitchen", style: .Default) { (action) in
}
dropdown.addAction(kitchen)
let livingRoom = UIAlertAction(title: "Living Room", style: .Default) { (action) in
}
dropdown.addAction(livingRoom)
let bedroom = UIAlertAction(title: "Bedroom", style: .Default) { (action) in
}
dropdown.addAction(bedroom)
let bathroom = UIAlertAction(title: "Bathroom", style: .Default) { (action) in
}
dropdown.addAction(bathroom)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
dropdown.addAction(cancel)
dropdown.modalPresentationStyle = .Popover
dropdown.popoverPresentationController?.sourceRect = self.dropdownButton.frame
dropdown.popoverPresentationController?.sourceView = self.dropdownButton
dropdown.popoverPresentationController?.permittedArrowDirections = .Up
self.presentViewController(dropdown, animated: true, completion: nil)
But the arrow does not appear and the UIAlertController still appears at the bottom. What Am I doing wrong?
popoverPresentationController (and it's settings) are meant to be used by a UIViewController who's presentationStyle is UIModalPresentationPopover.
While UIAlertController is a subclass of UIViewController, it is special and uses UIModalPresentationCustom as it's presentationStyle. Attempts to set a new presentationStyle are ignored.
I don't think what you are trying to do is an intended use for UIAlertController. You best bet is to make your own view controller to achieve this purpose.
The default text on the button for a user to confirm a picture when using the .Camera option for the UIImagePicker is "Use Picture".
Is there any way to change this to some other string?
func chooseAlert(){
let alert = UIAlertController(title: "Upload Image", message: "Choose Source For Image.", preferredStyle: .ActionSheet)
let cameraAction = UIAlertAction(title: "Take Photo", style: .Default, handler: { (action:UIAlertAction) -> Void in
dispatch_async(dispatch_get_main_queue())
{
// self.performSelector(#selector(self.useCamera), withObject: nil, afterDelay: 1.0)
self.useCamera()
}
})
let cameraRollAction = UIAlertAction(title: "Use Photo", style: .Default, handler: { (action:UIAlertAction) -> Void in
dispatch_async(dispatch_get_main_queue())
{
self.useCameraRoll()
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel){
(action) in
print(action)
}
alert.addAction(cameraAction)
alert.addAction(cameraRollAction)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
In the IBAction of the Button call this function which will bring an alert on the screen. In the alert actions, change the title as per your wish. useCamera() and useCameraRoll() are user defined functions.
I'm quite new to Swift (and coding in general) and to this website.
I'm having a little bit of an issue now. In my app, I have an alert when a timer reaches 0. In that alert, there are 2 buttons. One says "Share" and the other says "Continue". I want to make it such that when a user taps "Continue", the next Storyboard will be shown to them. As of now, whatever button I press will dismiss the alert, but stay on the same Storyboard. (It also prints to the console which button I pressed, but of course that's just for me to see).
How do I go about doing this? Here is my code, in case anyone wants to know.
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button one")
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button two")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
presentViewController(alert, animated: true, completion:nil)
Try with this:
// Create the alert controller
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
//do whatever you want here
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
If you haven't already set your ViewControllerIdentifier:
You need to set the Identifier value that you can find on the right column
Here is the code that might help you. When you create a AlertController with actions, you can provide the actions and style of the button when you define them. In code below action is in the closure (Block) and style is defined as .Default or .Cancel
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
// Action when you press button goes here
print("Here you show next storyboard item.")
// Code to push new ViewController. For example :
self.navigationController?.pushViewController(newVCInstance, animated: true)
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
print("Here you share things.")
// Code to share things.
}
let thirdAction = UIAlertAction(title: "Cancel", style: . Cancel) { (alert: UIAlertAction!) -> Void in
print("Just dismissing the Alert Controller.")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
alert.addAction(thirdAction)
presentViewController(alert, animated: true, completion:nil)
I would like to create the following UIAlertAction:
#IBAction func buttonUpgrade(sender: AnyObject) {
let alertController = UIAlertController(title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
}
I'm aware that an UIAlertController is initialized with a title, message, and whether it prefers to be displayed as an alert or action sheet.
When the button is pressed I would like to display the alert, but alert.show() doesn't work. Why doesn't my code work?
The main issue here is that UIAlertController (unlike UIAlertView) is a subclass of UIViewControlller, meaning it needs to be presented as such (and not via the show() method). Other than that, if you want to change to color of the cancel button to red, you have to set the cancel action's alert style to .Destructive.
This only works if you want the button to be red. If you want to change the colors of the buttons in the alert controller to arbitrary colors, this can only be done by setting the tintColor property on the alert controller's view property, which will change the tint color of all of its buttons (except those that are destructive). It should be noted that with the design paradigms that Apple has put in place, it isn't necessary to change the cancel button's color due to the implications of its having bolded text.
If you do still want the text to be red though, it can be done like this:
let alertController = UIAlertController(
title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert
)
let cancelAction = UIAlertAction(
title: "Cancel",
style: UIAlertActionStyle.Destructive) { (action) in
// ...
}
let confirmAction = UIAlertAction(
title: "OK", style: UIAlertActionStyle.Default) { (action) in
// ...
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
Which produces the results you're after:
let alertController = UIAlertController(
title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert
)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
let okayAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(okayAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true) {
// ...
}
var alertController = UIAlertController(title: "Alert", message:"Message", preferredStyle: UIAlertControllerStyle.Alert)
let confirmed = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(confirmed)
alertController.addAction(cancel)
self.presentViewController(alertController, animated: true, completion: nil)
Important is the last line "self.presentViewController" to actually show your alert.
Hope it works ;)