Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController not working in iOS 9

I have added the UIAlertController code showing login and password textfields, it works for iOS 8 but in iOS 9 not works. The textfields shrinks as shown in figure below enter image description here

The code I am trying is as follows :

 - (void)toggleLoginLdap:(UIViewController *)currentVC
 {

 if ([UIAlertController class])
        {


            self.alertController= [UIAlertController
                                       alertControllerWithTitle:@"Title of Msg"
                                       message:@"Hello"
                                       preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action){
                                                           NSString *userName = self.alertController.textFields[0].text;
                                                           NSString *password = self.alertController.textFields[1].text;



                                                       }];
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                           handler:^(UIAlertAction * action) {


                                                               [self.alertController dismissViewControllerAnimated:YES completion:nil];

                                                           }];


            [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                 textField.placeholder = @"Username/Email";
                 //textField.preservesSuperviewLayoutMargins = YES;
                textField.autoresizesSubviews = YES;
            }];
            [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                textField.placeholder = @"Password";
                textField.secureTextEntry = YES;
            }];
            //alertController.view.autoresizesSubviews = YES;
            [self.alertController addAction:ok];
            [self.alertController addAction:cancel];
            [currentVC presentViewController:self.alertController animated:YES completion:nil];
 }

Also tried to show on rootviewcontroller but no luck , same code works on ios 8. Project is old and having support from iOS 5. Any help will be appreciated. Thanks.

like image 954
The iCoder Avatar asked Nov 09 '22 04:11

The iCoder


1 Answers

add [self.alertController.view layoutIfNeeded] after presenting alertController I had the same problem and it worked

like image 152
siemian Avatar answered Nov 30 '22 03:11

siemian