It looks like iOS 8 has a bug where alerts with text input do not show the keyboard. I have tried this hack.
The problem with the hack is that first the alert appears, and only afterwards does the keyboard appear. This causes the alert to "jump up" to make space for the keyboard.
How can I have a UIAlertView with text input, where the keyboard appears immediately?
(Note: For an example of what I want, go to Voice Memos, record a new memo, save, and you'll be prompted to enter a name with a UIAlertView with text input. There, the keyboard appears at the same time as the UIAlertView.)
I am not sure this will perfectly solve the problem of keyboard and alertView appearing simultaneously. But I would recommend you to use the newer api. I am posting this as an answer as its difficult to put code into comments.
For some reasons UIAlertView has been deprecated in iOS 8. Instead of using the UIAlertView you should use the UIAlertController with style UIAlertControllerStyleAlert. Present it and then open keyboard.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:yourTitle message:yourMessage preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
Since the alert now is shown as UIViewController the keyboard won't shift the alert box upside.
//ctrl+k to appear keyboard and ios8 keyboard appearing simultaneously issue would solve by this...
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"" message:#"Registration Successfully" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// [self.view endEditing:YES];
// [self.navigationController popToRootViewControllerAnimated:YES];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
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];
At the end of a process of doing backup I want to let the user know the backup is complete. I used to use UIAlertView, which works. However its depreciated so was swapping out these for UIAlertController. After the popup message is complete the window closes. The new UIAlertController doesnt seem to work in this situation. What am I doing wrong?
This is happening and the END of the process right before the view closes. The last line in the code below is closing the view. UIAlertView must be modal or something that stops the view from closing?
I used to use this code and it worked great.
UIAlertView *myalert = [[UIAlertView alloc] initWithTitle:nil message:Msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[myalert show];
[self presentViewController:alert animated:YES completion:nil];
And this new code you dont see anything
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#""
message:Msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
This line looks wrong:
[self dismissViewControllerAnimated:NO completion:nil];
Try removing that or putting it somewhere more suitable. I'd suggest that you are immediately presenting and then dismissing the alert.
[self dismissViewControllerAnimated:NO completion:nil];
Remove this line as you are presenting alert and quickly dismissing the View in which the alert was presented.
In short you are showing alert and removing the view on which the alert was shown
With the latest iOS 8.3 release, our app starts to have a weird behavior.
After finishing textfield editing, the user can click the close button which brings up an UIAlertView. When the user clicks discard in the alertview, alertview and current view are dismissed. But somehow the keyboard shows up after the view is gone which is quite annoying to users.
After some debugging, it seems that the keyboard is shown for the last textfield that the user has accessed before closing the view. I tried various ways to endEditing for the current view in many places (before showing UIAlertView, after clicking a button in the UIAlertView; I even set the focus to another UI element of the view). It didn't solve the problem.
But for this particular issue, I'm not sure if it's a common issue or something we need to fix. Everything works perfectly before iOS 8.3.
We understand that UIAlertView is deprecated for iOS 8. We're starting to migrate to UIAlertController. But if there's any workaround, we'd love to hear.
Here's some code snippet.
- (IBAction)closeTapped:(UIButton *)sender
{
// try to resign first responder
// [self.tfName resignFirstResponder];
// [self.tfPosition resignFirstResponder];
[self.view endEditing:YES];
if(self.orderDetails.isOpen && self.orderItemChanged)
{
UIAlertView* saveAlert = [[UIAlertView alloc] initWithTitle:#"Unsaved Changes"
message:#"Your changes have not been saved. Discard changes?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", #"Discard", nil];
[saveAlert show];
}
else
{
[self close];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex)
{
case 1: // Save
{
[self save];
break;
}
case 2: // Discard
{
[self close];
break;
}
}
}
- (void)close
{
[self.delegate dismissEditOrderItemVC];
}
If your deployment target is iOS 8+, try UIAlertController.
Here's a quick fix for UIAlertView: delay the invocation of showing the alert view when your text field or text view resigns first responder.
[self performSelector:#selector(showAlertView) withObject:nil afterDelay:0.6];
If anyone struggles with this, I hope this will help:
if (NSClassFromString(#"UIAlertController")) {
UIAlertController* alert = ...
}
else {
UIAlertView* alert = ...
}
you need to change alert for ios 8.3
first put this in your view
#define IS_IOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0
then
if (IS_IOS8) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:#"Unsaved Changes" message:#"Your changes have not been saved. Discard changes?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *saveAction = [UIAlertAction
actionWithTitle:#"Save"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self save];
}];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *discardAction = [UIAlertAction
actionWithTitle:#"Discard"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
[alertVC addAction:saveAction];
[alertVC addAction:cancelAction];
[alertVC addAction:discardAction];
[self.view.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
this will help you as it helps me in same problem.
above code is compatible with both ios 7 & 8
I too, had a keyboard popping up (with the cursor in the last-used textView) after closing a UIAlertController and here is a very simple fix:
Immediately before building and presenting the UIAlertController,
Using [_activeTextView resignFirstResponder]; the keyboard will reappear.
Using [self.view endEditing:YES]; the keyboard will NOT reappear.
I hope this helps you.
Try using the below code. It works fine for iOS 8 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
[alertVC addAction:cancelAction];
[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
}];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
If a text field is the first responder, it will automatically pop up the keyboard when the alert is dismissed. Ensure the first responder is properly dismissed with:
[textField resignFirstResponder]
Remember: in a table or scroll view, sometimes the field must be visible on the screen to properly dismiss the responder.
If there are no first responders active, keyboard should not appear when alert is dismissed.
For the particular case in this question, I would recommend setting a delegate method to listen for the "done" button and resigning the first responder in the delegate callback.
Alternatively, when beginning editing, you can store a reference to the currently active text field, then in your "clickedButtonAtIndex" method you can resign the active text field if it is still active.
I've noticed some weird behavior with textField keyboards and alertViews as well... Maybe make a bool called disableKeyboard and use it like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (disableKeyBoard) {
disableKeyboard = NO;
return NO;
} else {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
disableKeyboard = YES;
}
This is just a workaround and doesn't address the core issue, whatever it is. For this method to work you need to set the alertView and textField delegate methods in your header.
Issue
I'm having issues with UIAlertController. It's an iPad project, in that I have two classes, say First and Second.
I'm presenting Second from First, using a modal presentation. In Second Class I have a button, clicking on that I'm showing UIActionSheet to perform some actions. It works perfectly except if user clicks on the button rapidly the Second class is being dismissed.
Code
First VC:
- (IBAction)showNext:(id)sender
{
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
[self presentViewController:vc animated:YES completion:nil];
}
Second VC:
- (IBAction)showActionSheet:(UIButton *)sender
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC addAction:[UIAlertAction actionWithTitle:#"LogOut"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:#"Update"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}]];
[alertVC setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertVC popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds;
[self presentViewController:alertVC animated:YES completion:nil];
}
No other code in both classes. I don't know why it happens, Actually it happened to one of my Client project, So I created a test app with above code and it's also causing the issue.
Screen Capture
What I have tried:
I checked UIAlertController Class Reference
Did a search on Google,, nothing came up
Alternatives:
If I set Second class as my rootViewController, the issue won't be there (But I can't do that, I need to navigate back and forth)
If I disable the popover dismissal when touched outside, it can avoid the issue. But I need this workflow.
Can anybody please help me ? I'm totally lost at this moment, not getting any useful info.
Thanks in advance.
I don't know what is the issue, but I fixed it. I used popoverPresentationControllerShouldDismissPopover and returned YES.
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
{
return YES;
}
Eureka:
It was an accident!!! I didn't get any other way to fix this issue, so I thought will do the second alternative. While writing I accidentally wrote YES instead of NO. But it worked, I don't know how it worked and what is the reason for that issue.
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.