iOS 8.3 UIAlertController crashes when trying to add a textfield

后端 未结 12 1558
暗喜
暗喜 2020-12-30 03:44

I have an iPad app. I am creating an UIAlertController and adding a textfield. It crashes. It only crashes when I add a textfield.

let alert = UIAlertContr         


        
相关标签:
12条回答
  • 2020-12-30 04:30

    This fixed it immediately (for me). I had the exact same error that Chris described :

    NSInteralInconsistencyException - UIKeyboardLayoutAlignmentView

    The answer was simply to make sure animated is set to "NO" when presenting the UIAlertController.

    [self presentViewController:alert animated:NO completion:nil];
    
    0 讨论(0)
  • 2020-12-30 04:31

    This seems to have something to do with styling the UIAlertController's view, as eluded to by @BigShay.

    In my example I set the tintColor of the view before adding the UITextField and I crash in iOS 8. The workaround given by @Baza207 to add the UITextField after displaying the alert avoids the crash in iOS 8, however it also results in no text field appearing at all in iOS 9.

    If you just move the styling to after adding the text field, it works with both iOS 8 and iOS 9 (no crash in iOS 8, and no missing text field in iOS 9):

    Crash in iOS 8

    Works in iOS 9

    alertController.view.tintColor = UIColor.blueColor()
    
    alertController.addTextFieldWithConfigurationHandler { textField in
    }
    
    presentViewController(alertController, animated: true, completion: nil)
    

    Works in iOS 8

    No text text field in iOS 9

    alertController.view.tintColor = UIColor.blueColor()
    
    presentViewController(alertController, animated: true, completion: nil)
    
    alertController.addTextFieldWithConfigurationHandler { textField in
    }
    

    Works in iOS 8

    Works in iOS 9

    alertController.addTextFieldWithConfigurationHandler { textField in
    }
    
    alertController.view.tintColor = UIColor.blueColor()
    
    presentViewController(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-30 04:31

    I really didn't like the solutions I saw and came up with an easier one. The crash occurs (for me, at least) when the keyboard is visible and an alert with an input field is presented.

    I simply dismiss the keyboard prior to presenting the alert and then everything is fine.

    0 讨论(0)
  • 2020-12-30 04:36

    There does seem to a bug in iOS 8.3 related alerts. It manifests on both (deprecated) UIAlertView and the iOS8-only UIAlertController. When I attempt to add a textfield to either of these controllers, I get the following crash:

     *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The layout constraints still need update after sending -updateConstraints to <_UIKeyboardLayoutAlignmentView: 0x792d28e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x792d2ab0>>.
    _UIKeyboardLayoutAlignmentView or one of its superclasses may have overridden -updateConstraints without calling super. Or, something may have dirtied layout constraints in the middle of updating them.  Both are programming errors.'
    

    Alerts without textfields are OK, but showing a UIAlertView with style UIAlertViewStylePlainTextInput or showing a UIAlertController with a textfield added via addTextFieldWithConfigurationHandler will result in the above crash.

    The fix seems to be to set a prophylactic frame on the UIAlertController before calling show. This frame is overridden before show, but prevents the crash.

    if (NSClassFromString(@"UIAlertController")) {
        // iOS8
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert"
                                                                       message:@"Be alert, not alarmed"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.keyboardType = UIKeyboardTypeEmailAddress;
    
        }];
        alert.view.frame = CGRectMake(0.0, 0.0, 320.0, 400.0); // Workaround iOS8.3 bug - set this to larger than you'll need
    
        [self presentViewController:alert animated:YES completion:^{
            [alert.textFields[0] becomeFirstResponder];
        }];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                        message:@"Be alert, not alarmed"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        UITextField *emailField = [alert textFieldAtIndex:0];
        emailField.keyboardType = UIKeyboardTypeEmailAddress;
    
        [alert show];
    }
    
    0 讨论(0)
  • 2020-12-30 04:37

    As @Dex mentioned, I fixed this by modifying my controller's view AFTER adding the text field. I was modifying the view's tint color.

    controller.view.tintColor = ...some color...
    

    Moving this to "after" adding the text field fixed it for me. Accepted answer and other suggestions led to other bugs for me. Sorry, I don't have the rep to post a comment yet on @Dex's answer.

    0 讨论(0)
  • Aha, I think this may be answered by jcesarmobile in another thread: UIAlertController/UIAlertView scrolling on iOS 8

    The workaround a lot of folks added to deal with prior bugs with long text in UIAlertController now causes this crash in 8.3. And the long text issue is fixed in 8.3, so the workaround can be made conditional to avoid this in 8.3

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