UIAlertController not working in iOS 9

2019-05-02 23:11发布

问题:

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

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.

回答1:

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



回答2:

I know this question is old but I've been researching this for more than a day.

I stumbled upon a thread in the Pixate freestyle github. My problem was that we were using Pixate Freestyle pod

If you take your code and put it in a clean Xcode project, it will look fine. There was a workaround mentioned there; but it's in swift. I'll try to find it and post a link when I do.

EDIT: https://github.com/Pixate/pixate-freestyle-ios/issues/178