iOS 8.3 UIAlertController crashes when trying to add a textfield

后端 未结 12 1555
暗喜
暗喜 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: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 = >.
    _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];
    }
    

提交回复
热议问题