Can we customize buttons of UIAlertviewController in swift? - swift

I am trying to customize buttons of UIAlertView but however I am unable to achieve it, I am new to swift and I don't how to achieve.
Thank you so much.

Simple answer is no you can not customise default alertViewController buttons but you can achieve it by using custom alertView.
HERE is some available libraries you can use.

To followup on #Dharmesh Kheni's answer, while you get no additional functionality for UIAlertViewController in swift, you can still use libraries in your swift code.
I work for Urban Outfitters and we have an open source customizable alert, URBNAlert
Here is some code, using URBNAlert in swift
let alert = URBNAlertViewController(title: "My Title", message: "Message")
alert.alertStyler.backgroundColor = UIColor.lightGrayColor()
alert.alertStyler.buttonHeight = 44
alert.alertStyler.buttonTitleColor = UIColor.redColor()
alert.alertStyler.cancelButtonBackgroundColor = UIColor.greenColor()
// many, many more things you can customize.
alert.addAction(URBNAlertAction(title: "Button Title", actionType: .Normal, actionCompleted: { (action) -> Void in
// do some stuff here when the user selects this button
}))
alert.show()

Related

Disable the auto dismissal of the alert when tapping on a button

func showAlert(...) {
let alertController = UIAlertController(...)
let add = UIAlertAction(title: "Add", style: .default) { (action) in
onAdd?()
}
alertController.addAction(ok)
...
}
I would like to keep the popup alert after click on Add. But it seem iOS will auto close the popup. How to make it work?
The only way to prevent a UIAlertController's alert from being dismissed when a button is tapped is to disable the button.
If you don't like that, don't use UIAlertController; use your own presented view controller. That's all a UIAlertController is, after all. So it's easy to write your own if you need to.

Swift : OverlayView that appears at the bottom of the screen

I'd like to create a very common effect as in the picture below :
Explanation : the effect I'd like to accomplish consists in a view that appears (slides in) at the bottom of the screen when user clicks a button : you can still see the screen behind this view, but it applies a "dark layer" (black view with let's say 60% opacity) on the top of it. When user clicks on Block or Report absue (as for this example) it would perform the respective actions. Now when user clicks on cancel, and also when he clicks anywhere on the "dark layer" it would bring him back to the screen.
What I tried : presenting a new view controller (but it would use more data than necessary), using a overlay layer but I even didnt get close to that effect that we're usually seeing on apps. I'm not sure but I'd say that the best way to get that effect is using views ?
Does anyone have an idea please ?
Thanks and have a good day,
J
You are looking for a native element called UIActionSheet. It has become the part of UIAlertController, and does exactly what you are looking for.
Here is a little help how to set up one:
// Create you actionsheet - preferredStyle: .actionSheet
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Create your actions - take a look at different style attributes
let reportAction = UIAlertAction(title: "Report abuse", style: .default) { (action) in
// observe it in the buttons block, what button has been pressed
print("didPress report abuse")
}
let blockAction = UIAlertAction(title: "Block", style: .destructive) { (action) in
print("didPress block")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("didPress cancel")
}
// Add the actions to your actionSheet
actionSheet.addAction(reportAction)
actionSheet.addAction(blockAction)
actionSheet.addAction(cancelAction)
// Present the controller
self.present(actionSheet, animated: true, completion: nil)
It should produce the following output:

Present Pop-up UIAlertController from within UITableViewCell Without UITableView

In my iOS app I have a UIViewController ViewController that presents a view that is partly made up of a TableView. The UITableViewCells include a UISegmentControl. When the UISegmentControl's valueHasChanged action is called, I want to present a pop-up. This is the code called within the action:
//Check that the text field isn't empty:
if self.TextField.text == "" {
self.Status.selectedSegmentIndex = 0
let viewController = ViewController()
viewController.alertStatusChangedForEmptyTextField(self)
}
This is the pop-up I have created within the ViewController class that checks if an adjacent text field in the same UITableViewCell is empty or not:
//Alert function that handles when toggle is set even though there is no content.
func alertStatusChangedForEmptyTextField(sender: AnyObject) {
let alert = UIAlertController(title: "Text field has no contents", message: "Please enter a text.", preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil));
showViewController(alert, sender: self);
}
I get an error that pops up in the viewDidLoad function of the ViewController, but originates from the showViewController method call within this function (I know because I've tried commenting out only this last line and then everything works fine).
How can I call a pop-up(ViewController) from within a UITableViewCell? Happy to hear suggestions that involve completely different approaches (Creating a custom UITableView subclass? A function that is defined within the TableViewCell class?)
Also, I don't have a root view controller.
check the value of storyboard, there is a good chance that you will find it nil;
storyboard.instantiateViewControllerWithIdentifier("PopMenuViewController") as! PopMenuViewController
I am trying to figure out how to present the pop up from custom tableview cell element. If you figured it out than do share the method :)

Swift NSMenuItem detect shift click

I'm currently adding an NSMenuItem to my NSMenu and I'm able to successfully detect and handle the click event. However, I want to add a second option and detect if the user has clicked the menuItem while holding SHIFT
let menuItem = myMenu?.submenu?.addItemWithTitle("Click me", action: "itemClicked:", keyEquivalent: "")
Is there any way to do this? I looked at keyEquivalent but I'm not seeing ANYTHING to how to get this to work.
Thanks
Thanks to #LeoDabus for some help but I was able to solve this in the event handler for my NSMenuItem with the following code:
if let event = NSApp.currentEvent {
if event.modifierFlags.contains(.ControlKeyMask) {
// success!
}
}
happy coding

iOS 8 showFromToolbar effect with UIActionController

UIActionController replaced UIActionSheet in iOS 8. Is there any way to recreate the showFromToolbar behavior of UIActionSheet in UIActionController? This is the effect I am seeking. Please note though that I am trying to draw a UIActionController coming out of an external action bar button on an iPhone, so I couldn't do this using generic UIActionSheet example too I suppose.
I created a UIBarButtonItem and, in its IBAction I am creating a UIActionController. I tried the following:
var alert = UIAlertController(title: "XYZ", message: "XYZ", preferredStyle: .Alert
// Then created a bunch of UIAlertAction items and added them to alert
var popover = alert.popoverPresentationController
if popover != nil {
var s = sender as UIBarButtonItem
popover?.sourceView = s.customView
popover?.sourceRect = s.customView!.bounds
popover?.permittedArrowDirections = .Any
} else {
println("POPOVER is nil")
}
self.presentViewController(alert, animated: true, completion: nil)
However, this doesn't work with either iPhone or iPad simulators - the alert is showing in middle of screen and the above code always prints "POPOVER is nil". Even if it would have worked in iPad it wouldn't have solved my original problem. Your advice is appreciated.
Found this wonderful tutorial that explains how to do it.