UIAlertView changes to landscape when App is locke

2019-02-16 23:50发布

问题:

I have an app and its orientation is locked as portrait mode, but when I have a UIAlertView and turn the iPhone to landscape, the alert changes the orientation and crops the alert's overlay. Only in iOS8.

Anyone had this error?

回答1:

Yes I did get this issue. I have developed my code in iOS7 but had to do compatible with iOS 8 also. So I came across This link. I got my solution as follows:

When you are working with iOS7 UIAlertView works fine but if you are working with iOS8 you have to use UIAlertController. Use this kind of code:

if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
 {
   alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Invalid Coupon."  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
 }
        else
       {
        UIAlertController *alertController = [UIAlertController    alertControllerWithTitle:@"Warning" message:@"Invalid Coupon." preferredStyle:UIAlertControllerStyleAlert];


        UIAlertAction *okAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action)
                                   {

                                   }];

        [alertController addAction:okAction];

        [self presentViewController:alertController animated:YES completion:nil];
    }

Hope this will help you.