Customize actionsheet IOS swift - ios

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)

Related

UIAlertController does not disable controls of its presenter UIViewController

I am inside a child UIViewController. I present a UIAlertController using the following code:
let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in
}
alertController.addAction(OKAction)
self?.present(alertController, animated: true)
The problem is that when the alert is presented, I can still tap and edit the controls of the presenter view controller.
What changed the default behavior of blocking the parent (presenter) view controller when presenting the alert?

iOS SlideUp menu for phone numbers

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)

Facebook image pop up menu

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.

UIAlertController Gets Deallocated Before Presenting It

I want to make a "rate app" alert, but for some reason it gets deallocated before showing it.
Here's the code:
func showAlert() {
if #available(iOS 8.0, *)
{
let alertController = UIAlertController(title: "Rate App", message: "Rate this app now", preferredStyle: .Alert)
let neverAction = UIAlertAction(title: "Never Show This Again", style: .Destructive, handler: { (action: UIAlertAction) in
self.userDefaults.setBool(true, forKey: "rateAlertRejected")
})
let rateAction = UIAlertAction(title: "Rate Now", style: .Default, handler: { (action: UIAlertAction) in
// Rate App
})
let remindAction = UIAlertAction(title: "Remind Me Later", style: .Cancel, handler: nil)
alertController.addAction(rateAction)
alertController.addAction(neverAction)
alertController.addAction(remindAction)
presentViewController(alertController, animated: true, completion: nil)
}
else
{
// Identical code (using UIAlertView) for iOS 7 which works perfectly
}
}
The method executes (in certain conditions, but for testing purposes it does it every time) after a custom unwind segue.
Why do I have this problem? I used UIAlertController before but I had no issues.
According to your comment you show showAlert in
a method that executes after a custom unwind segue.
unwind segue dismisses the view heriarchy and therefore your alert
does not get reference to a view controller to show from.
To solve this, show your alert in the View controller you unwind to or wait for the alert controller action to be completed before unwinding.

What is the name of this popover class?

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.

Resources