简体   繁体   中英

LayoutConstraints error without using Auto Layout

I have not used Auto Layout in my storyboard or implemented any code for this. I have a UIAlertController for user login and get a LayoutConstraints error during the keyboard text entry.

Important to note that this error does not occur when I run the app on an actual device eg iPad, only occurs in the simulator.

I am using Xcode Version 10.1 (10B61) and have placed a UIViewAlertForUnsatisfiableConstraints breakpoint in Debug which indicates a number of constraint errors.

After stepping through all the breakpoints I get this message in the console:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x60000165fe80 h=--& v=--& UIKeyboardAssistantBar:0x7f922cd1aa30.height == 0   (active)>",
    "<NSLayoutConstraint:0x6000016768f0 V:|-(0)-[_UIButtonBarStackView:0x7f922cd1d6e0]   (active, names: '|':UIKeyboardAssistantBar:0x7f922cd1aa30 )>",
    "<NSLayoutConstraint:0x600001676a80 V:[_UIButtonBarStackView:0x7f922cd1d6e0]-(0)-|   (active, names: '|':UIKeyboardAssistantBar:0x7f922cd1aa30 )>",
    "<NSLayoutConstraint:0x60000165f340 'UIButtonBar.maximumAlignmentSize' _UIButtonBarButton:0x7f922cf87400.height == UILayoutGuide:0x600000c39260'UIViewLayoutMarginsGuide'.height   (active)>",
    "<NSLayoutConstraint:0x6000016764e0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600000c39260'UIViewLayoutMarginsGuide']-(9)-|   (active, names: '|':_UIButtonBarStackView:0x7f922cd1d6e0 )>",
    "<NSLayoutConstraint:0x600001676440 'UIView-topMargin-guide-constraint' V:|-(10)-[UILayoutGuide:0x600000c39260'UIViewLayoutMarginsGuide']   (active, names: '|':_UIButtonBarStackView:0x7f922cd1d6e0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000016764e0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600000c39260'UIViewLayoutMarginsGuide']-(9)-|   (active, names: '|':_UIButtonBarStackView:0x7f922cd1d6e0 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

I have painstakingly gone through my storyboard and found no Auto Layout contstraints.

I implemented code in each UIView to disable constraints [self.view removeConstraints:self.view.constraints];

Could this be an Apple bug since it does not occur on the actual device?

Here is the code for the user prompt to login and keyboard input via a UIAlertController :

-(void) promptUserLogin
{
    NSString *alertTitle = @"User Login Required";
    NSString *alertMessage = @"Enter Email and Password";

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
                                                                             message:alertMessage
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
     {
         textField.placeholder = @"User Email";
         [textField addTarget:self action:@selector(alertTextFieldDidChange:)
             forControlEvents:UIControlEventEditingChanged];
     }];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
     {
         textField.placeholder = @"User Password";
         textField.secureTextEntry = YES;
     }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: @"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action)
                                   {
                                       NSLog(@"Cancel action");
                                   }];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle: @"OK"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action)
                               {
                                   UITextField *login = alertController.textFields.firstObject;
                                   UITextField *password = alertController.textFields.lastObject;

                                   NSLog(@"OK Action");
                                   NSLog(@"Login String: %@",login.text);
                                   NSLog(@"Password String: %@",password.text);
                               }];

    okAction.enabled = NO;
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];

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

- (void)alertTextFieldDidChange:(UITextField *)sender
{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController)
    {
        UITextField *login = alertController.textFields.firstObject;
        UIAlertAction *okAction = alertController.actions.lastObject;
        okAction.enabled = login.text.length > 2;
    }
}

I don't believe this has anything to do with your own layout issues. It's related to the InputAssistantItems (above the keyboard) failing auto layout constraints. If you look at the first autoresizing constraint listed in your auto layout errors it references the UIKeyboardAssistantBar.

A bit more detail as to the ways you can reproduce it:

When using a UITextField who's inputAssistantItem won't contain any autocorrection suggestions (either it's a secure text field or it's autocorrectionType is set to UIAutoCorrectionTypeNo) it breaks auto-layout constraints (I'm only able to reproduce it in certain scenarios when switching between UITextFields or resigning/becoming active).

This occurs in 2 scenarios:

  1. The textfield has it's autocorrectionType set to UITextAutoCorrectionTypeNo.
  2. The textfield is a Secure TextField.

In both cases the center UIButtonBarStackView would be empty (contain no autocorrect suggestions).

The issue isn't present when you either disable the inputAssistantItem by setting the leading/trailing BarButtonGroups to an empty array or use a textfield with auto-correction enabled.

Edit:

Just to elaborate a bit, I'm not suggesting either of the alternatives above to get rid of the issue, because you're most likely seeing it with a Secure Text Field since you reference a login. Instead my recommendation is to ignore the warning, and report it as a bug if you feel that's appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM