i had tried to search for the way to achieve the menu through google/youtube etc and still can't found a way to achieve this.
How can i achieve the pop up menu almost like this? :-
any suggestion would be much appreciated.
You can use this code to get achieve this same. This is working with Swift 3 and XCode 8.
let actionSheet: UIAlertController = UIAlertController.init(title: "Title of the action sheet", message: "Message to display", preferredStyle: .actionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheet.addAction(cancelAction)
//Create and add first option action
let savePictureAction: UIAlertAction = UIAlertAction(title: "Using Camera", style: .default) { action -> Void in
// Write Save Method Here
}
actionSheet.addAction(savePictureAction)
//Create and add a second option action
let sendInMessenger: UIAlertAction = UIAlertAction(title: "Choose Existing Photo", style: .default) { action -> Void in
// Write your code to Send In Messenger Here
}
actionSheet.addAction(sendInMessanger)
//Create and add a third option action
let dontLikePhoto: UIAlertAction = UIAlertAction(title: "Choose Existing Photo", style: .default) { action -> Void in
// Write your code if you Don't like photo Here
}
actionSheet.addAction(dontLikePhoto)
//Create and add a third option action
let trunOnNotification: UIAlertAction = UIAlertAction(title: "Choose Existing Photo", style: .default) { action -> Void in
// Write your code Turn on Notifications Here
}
actionSheet.addAction(trunOnNotification)
//We need to provide a popover sourceView when using it on iPad
actionSheet.popoverPresentationController?.sourceView = sender as? UIView
//Present the AlertController
self.present(actionSheet, animated: true, completion: nil)
Please note that this method is available from iOS 9.
Related
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)
How do customize actionsheet default when click button upload file on webview in ios app with swift?
Here the action sheet variable is the actionsheet in question. You can give it a title and a message.
You then have to create a variable for each action that you want with the UIAlertAction initializer, in there handlers, you can add what each action should do.
You then need to assign the actions to the Action Sheet that you created.
Finally, you should present the action sheet from the view controller that is in charge of presenting it.
The following does what I'm trying to explain.
let actionSheet = UIAlertController(title: "My Action Sheet", message: "My Action Sheet Message", preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
//Perform any actions specific to action 1 in your class
}
let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
//Perform any actions specific to action 2 in your class
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) //Will just dismiss the action sheet
actionSheet.addAction(action1)
actionSheet.addAction(action2)
actionSheet.addAction(cancelAction)
present(actionSheet, 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.
I cannot find the name of the class for this 'popover'. Apple uses it a lot in their applications.
I've searched for popover, NSAlert, custom hidden/visible views, and many more.
What is it called?
This is UIAlertController. Before ios7 it is know as UIActionSheet
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts.
#IBAction func showActionSheetTapped(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
//Code for launching the camera goes here
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
//Code for picking from camera roll goes here
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Output:
May be it will help you.
Couldn't find a clear and informative explanation for this.
After searching a while on a subject I didn't
find a clear explanation , even in it's class reference
UIAlertController Reference
It is ok, but not clear enough for me.
So after collecting some peaces I decided to make my own explanation
(Hope it helps)
So here it goes:
UIAlertView is deprecated as pointed out :
UIAlertView in Swift
UIAlertController should be used in iOS8+
so to create one first we need to instantiate it,
the Constructor(init) gets 3 parameters:
2.1 title:String -> big-bold text to display on the top of alert's dialog box
2.2 message:String -> smaller text (pretty much explains it's self)
2.3 prefferedStyle:UIAlertControllerStyle -> define the dialog box style, in most cases: UIAlertControllerStyle.Alert
Now to actually show it to the user, we can use showViewController or presentViewController and pass our alert as parameter
To add some interaction with a user we can use:
4.1
UIAlertController.addAction to create buttons
4.2
UIAlertController.addTextField to create text fields
Edit note: code examples below, updated for swift 3 syntax
Example 1: Simple Dialog
#IBAction func alert1(sender: UIButton) {
//simple alert dialog
let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);
//show it
show(alert, sender: self);
}
Example 2: Dialog with one input textField & two buttons
#IBAction func alert2(sender: UIButton) {
//Dialog with one input textField & two buttons
let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);
//default input textField (no configuration...)
alert.addTextField(configurationHandler: nil);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
let fields = alert.textFields!;
print("Yes we can: "+fields[0].text!);
}));
present(alert, animated: true, completion: nil);
}
Example 3: One customized input textField & one button
#IBAction func alert3(sender: UIButton) {
// one input & one button
let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);
//configured input textField
var field:UITextField?;// operator ? because it's been initialized later
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder="I am displayed, when there is no value ;-)";
input.clearButtonMode=UITextFieldViewMode.whileEditing;
field=input;//assign to outside variable(for later reference)
});
//alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later
func yesHandler(actionTarget: UIAlertAction){
print("YES -> !!");
//print text from 'field' which refer to relevant input now
print(field!.text!);//operator ! because it's Optional here
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));
present(alert, animated: true, completion: nil);
}
Hope It helps, and good luck ;-)
An instance of the UIAlertController can be presented modally on screen just as any other UIViewController using the presentViewController:animated:completion: method. What makes the UIAlertController instance differentiate between working as an ActionSheet or as an AlertView is the style parameter you pass when creating it.
No more delegation
If you have used a UIActionSheet or UIAlertView, you know that the way to get a callback from it is for a class (in most cases the ViewController) to implement the UIActionSheetDelegate or UIAlertViewDelegate protocol. There are some open source projects that replaced this delegation pattern with block based callbacks, but the official APIs were never updated. UIAlertController does not use delegation. Instead, it has a collection of UIAlertAction items, that use closures (or blocks if you are using Objective-C) to handle user input.
For Action Sheet
#IBAction func showActionSheet(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
//Code for launching the camera goes here
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
//Code for picking from camera roll goes here
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
For AlertView with Text Field
#IBAction func showAlert(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(nextAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Some of the syntax has changed since the original responses. Here is some sample code that alerts the user if they are not signed in to iCloud.
CKContainer.default().accountStatus { (accountStatus, error) in
switch accountStatus {
case .available:
print("iCloud Available")
case .noAccount:
print("No iCloud account")
//simple alert dialog
let alert=UIAlertController(title: "Sign in to iCloud", message: "This application requires iClound. Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID", preferredStyle: UIAlertControllerStyle.alert);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil));
//show it
self.present(alert, animated: true, completion: nil)
case .restricted:
print("iCloud restricted")
case .couldNotDetermine:
print("Unable to determine iCloud status")
}
}