UI issue with nil title in UIAlertView iOS 8 beta 5

前端 未结 4 1262
栀梦
栀梦 2021-02-13 10:41

I have an issue related to UIAlertView while running our app on iOS 8. I am showing an alert with title as nil. It was working fine in iOS 7 but now UI looks odd.

I ha

相关标签:
4条回答
  • 2021-02-13 11:13

    Please try this code. it is working on my side xcode version 9.2

    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:nil
                                  message:nil
                                  preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction* btn1 = [UIAlertAction
                           actionWithTitle:@"OKAY"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
    
                           }];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    0 讨论(0)
  • 2021-02-13 11:15

    The closest I could get with iOS 8 was by setting the title instead of the message:

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location field required." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    

    UIAlertView iOS8 title set

    It should be noted, however, that UIAlertView is deprecated in iOS 8 and, if you're going to be using separate code paths for iOS 7 and iOS 8, you should be using UIAlertController instead:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location field required."  message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
    

    I got the same results with both methods.

    0 讨论(0)
  • 2021-02-13 11:22

    It has been the best practice for me to use initWithTitle:@"" for UIAlertView, UIActionSheet since iOS 6 because I was facing a design issue during that time when I was using initWithTitle:nil. I tried to find back, I couldn't find it what exactly is the reason.

    From your screen shot on iOS 8, I think there is a change of view hierarchy on UIAlertView for iOS 8. I think Auto layout might be implemented on the view hierarachy as well as you can see the messageLabel jump up to the titleLabel.

    I can not be sure because the view hierarchy for UIAlertView is private.

    The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

    See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

    But, I use the code:-

    NSLog(@"%@",[self.alertView description]);
    

    Result on iOS 7.1:

    <UIAlertView: 0x7fb3c05535b0; frame = (18 263; 284 62); opaque = NO; layer = <CALayer: 0x7fb3c0519810>>
    

    Result on iOS 8.0:

    <UIAlertView: 0x7bf64840; frame = (0 0; 0 0); layer = <CALayer: 0x7bf648f0>>
    

    I am not sure why the UIAlertView frame for iOS 8 is (0 0; 0 0);

    Like Mike said, I think you should learn to use UIAlertController for iOS 8.

    0 讨论(0)
  • 2021-02-13 11:26

    I managed to get decent message alignment without the bold font by:

    1. Setting the title to @"" instead of nil, and
    2. (If IOS8) prepend a "\n" in front of the message.
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                                        message:@"\nLocation field required." 
                                        delegate:nil 
                                        cancelButtonTitle:@"OK" 
                                        otherButtonTitles:nil];
    

    enter image description here

    0 讨论(0)
提交回复
热议问题