This question already has answers here:
How to add date picker to UIAlertController(UIAlertView) in Swift?
(2 answers)
Closed 5 years ago.
I have a custom alert view controller to select date from date picker, but i'm confuse bit that how can i add two button in alert view controller and when user click the button than it shows the date picker in alert view controller and how can i change the color of alert view controller. My code is ,
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:picker];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"OK");
NSLog(#"%#",picker.date);
}];
action;
})];
UIPopoverPresentationController *popoverController = alertController.popoverPresentationController;
popoverController.sourceView = sender;
popoverController.sourceRect = [sender bounds];
[self presentViewController:alertController animated:YES completion:nil];
It looks like this,
If I remember well this question as already been asked few time , and each time the answer was "you should create a viewcontroller and use it as an alert" and , seeing what you need to do I also think it's a better way to do, + you can make your controller look like an alert adding a shadow...
See this answer : How to add date picker to UIAlertController(UIAlertView) in Swift?
Related
I am adding the alertView in whole application. and i want to change the size of ok or cancel button in alertView so that i can set the small image of actions.
Anyone please help me for this.
You cannot change the frame of default Alert view for this you have to use custom alert view. Please refer the below links for custom alert views
https://github.com/dogo/SCLAlertView
https://github.com/vikmeup/SCLAlertView-Swift (Swift)
https://github.com/mtonio91/AMSmoothAlert
You can not change the size of ok or cancel button in alertView.
The only solution that I could figure is to make a custom view with UIVisualEffect and show it like UIActionSheet
Welcome if other solution is there :)
if you want to do this for adding image, then try following way:
UIAlertController * view= [UIAlertController
alertControllerWithTitle:#"Add Image"
message:#"Image Added successfully"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* add = [UIAlertAction
actionWithTitle:#"add"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
[add setValue:[[UIImage imageNamed:#"add.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[view addAction:add];
[self presentViewController:view animated:YES completion:nil];
I currently have a UIAlertController that I need to make non-dismissible. The Alert should not dismiss when pressing the action button.
How can I do this?
UIAlertController *alert;
int bestScore = [[[NSUserDefaults standardUserDefaults] objectForKey:#"bestScore"] intValue];
if (!bestScore || bestScore < _score){
[[NSUserDefaults standardUserDefaults] setObject:#(_score) forKey:#"bestScore"];
alert = [UIAlertController alertControllerWithTitle:#"GAME OVER "
message:[NSString stringWithFormat:#"NEW RECORD! \n SCORE : %d \n\n\n\n\n\n", _score] preferredStyle:UIAlertControllerStyleActionSheet];
}
else alert = [UIAlertController alertControllerWithTitle:#"GAME OVER"
message:[NSString stringWithFormat:#"SCORE : %d \n Best score : %d \n\n\n\n\n\n ", _score, bestScore] preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:#"Try again" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self newGame];
[self addNewView];
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Answer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[alert viewWillDisappear:NO];
}]];
[self presentViewController:alert animated:YES completion:nil];
I would recommend creating a UIViewController that contains a UIView. Within this UIView you will be able to display your required information and add the custom button actions that you desire.
In order to make the UIView appear like a modal view controller add a UIVisualEffectView with a UIBlurEffectStyle.
This is as simple as creating a conventional view controller using your storyboard/xib adding the required UIView in the interface builder then linking up the associated view controller class. After completing the initial setup and user interface add the following code to viewDidLoad. Furthermore you can perform the required animations etc within viewWillAppear & viewWillDisappear.
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]
UIVisualEffectsView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
[self.view insertSubview:blurView atIndex:0];
You can do like this
If you dont want dismiss alertController then You can disabled button actions of it like
alert.actions[1].enabled = NO
This will make your alertController Non-dismissable.
simply present the alertContoller again at the start of the action for alertButton you want it to not dismiss,
[self presentViewController:alertController animated:YES completion:nil];
I'm trying to customize my UIAlertController with a dark theme.
I'm aiming for something like this
I'm trying different things, including the suggestion I found here https://stackoverflow.com/a/29122883/1817873, but for some reason only the first button gets colored while the cancel button keeps staying white.
Here is my code:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:#"Confirm" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:deleteAction];
[alert addAction:cancelAction];
UIView *firstView = alert.view.subviews.firstObject;
UIView *nextView = firstView.subviews.firstObject;
nextView.backgroundColor = [UIColor blackColor];
Any ideas?
Any ideas?
Yes. Give up. An alert controller is very limited, and you should stick to what it does. But it is just a presented view controller, and nothing stops you from making your own presented view controller that looks and acts just like an alert, and since it is your view controller, it's your view and you can do whatever you like.
If you're still struggling with this, I have a library that may be of help. It lets you create custom action sheets. It has a bunch of built-in types and can be extended and restyled as well.
I have a label which asks a questions and user needs to answer by tapping on "YES" or "NO" button. if user clicks directly on NEXT Button the alert pops up saying Choose Yes or No if user selects Yes then he gets a new sub question there itself , if user clicks on NEXT without answering the sub question another alertView should pop up. On both the Condition only one alert view is displayed. help plz
heres the code
-(IBAction)btnNextClicked:(id)sender {
activeButton = sender;
if (([_txtOwnerShipPercentage.text isEqualToString:#" "] || _txtOwnerShipPercentage.text==nil || _txtOwnerShipPercentage.text.length==0) && (activeButton.tag == 1)) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Warning" message:#"Select Ownership Percentage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
} else if (activeButton.tag == 0){
UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:#"Warning" message:#"Select 'YES' or 'No'" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController2 addAction:okAction];
[self presentViewController:alertController2 animated:YES completion:nil];
}
}
and i have set tags to YES and No Button... 1 & 2 respectively.
activebutton is nothing but globally declared UIButton
Here is the code of how you can manage two or multiple alert controllers in one context according to your requirement.
//declare first alert controller
UIAlertController *alertviewcontroller1 = [UIAlertController alertControllerWithTitle:#"My Alert 1"
message:#"This is an action sheet of alert 1"
preferredStyle:UIAlertControllerStyleActionSheet];
//declare second alert controller
UIAlertController *alertviewcontroller2 = [UIAlertController alertControllerWithTitle:#"My Alert 2"
message:#"This is an action sheet of alert 2"
preferredStyle:UIAlertControllerStyleActionSheet];
//declare first alert controller action with hendler
UIAlertAction *firstActionOfAlert1 = [UIAlertAction actionWithTitle:#"YES"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//Present second alert controller when user taps on 'YES' of first alert controller.
[self presentViewController:alertviewcontroller2 animated:YES completion:nil];
}];
//declare second alert controller action with hendler
UIAlertAction *firstActionOfAlert2 = [UIAlertAction actionWithTitle:#"YES"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
//add both actions relatively
[alertviewcontroller1 addAction:firstActionOfAlert1];
[alertviewcontroller1 addAction:firstActionOfAlert2];
//Present first alert controller
[self presentViewController:alertviewcontroller1 animated:YES completion:nil];
the above solution will not work in case if previous alert is not fully presented or without tapping an action in alertView. Use this method for that,
if (self.presentedViewController & !self.presentedViewController.isBeingPresented) {
[self.presentedViewController presentViewController:alert animated:YES completion:nil];
}
In iOS 7, I show actionSheet by "showFromRect":
[actionSheet showFromRect:rect inView:view animated:YES];
But in iOS 8, this doesn't work. They they replace the implementation and suggest us using UIAlertController. Then how do I show this actionSheet like a popover?
Using UIAlertController you can access the popoverPresentationController property to set the sourceView (aka inView) and sourceRect (aka fromRect). This gives the same appearance as the previous showFromRect:inView:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"message" preferredStyle:UIAlertControllerStyleActionSheet];
// Set the sourceView.
alert.popoverPresentationController.sourceView = self.mySubView;
// Set the sourceRect.
alert.popoverPresentationController.sourceRect = CGRectMake(50, 50, 10, 10);
// Create and add an Action.
UIAlertAction *anAction = [UIAlertAction actionWithTitle:#"Action Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(#"Action Pressed");
}];
[alert addAction:anAction];
// Show the Alert.
[self presentViewController:alert animated:YES completion:nil];
I also meet this problem like you, but my case is that I show a UIActionSheet in UIWindow.view on the iPad device, and the UIWindow doesn't set rootViewController.
So, I found, if we show a UIActionSheet in a window whose rootViewController equals to nil, the UIActionSheet could not show out.
My solution is to set
window.rootViewController = [[UIViewController alloc] init];
then,
[actionSheet showFromRect:rect inView:window.rootViewController.view animated:YES].
Hope this will help you!
according to this https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html thread
UIActionSheet is deprecated, you should use UIAlertController instead of UIActionSheet.
Thanks.
I found out the point is in iOS 7, you show actionSheet in a new window actually when using "showFromRect: inView:" ;
But in iOS 8 you show the actionSheet just on the view you send in parameters.
So the solution is to send
self.superView
[actionSheet showFromRect:rect inView:[self.superView] animated:YES];
Me and my colleague figured it out by randomly trying.