[英]LayoutConstraints error without using Auto Layout
我没有在情节提要中使用自动版式,也没有为此实现任何代码。 我有一个用于用户登录的UIAlertController
,并且在键盘文本输入期间出现LayoutConstraints
错误。
需要特别注意的是,当我在iPad等实际设备上运行该应用程序时,不会发生此错误,而只会在模拟器中发生。
我正在使用Xcode版本10.1(10B61),并在Debug中放置了一个UIViewAlertForUnsatisfiableConstraints
断点,该断点指示许多约束错误。
在完成所有断点之后,我在控制台中收到以下消息:
[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.
我辛苦地浏览了故事板,没有发现“自动布局”约束。
我在每个UIView
实现了禁用约束的代码[self.view removeConstraints:self.view.constraints];
由于它不是在实际设备上发生的,这可能是Apple的错误吗?
这是用户提示通过UIAlertController
登录和键盘输入的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;
}
}
我认为这与您自己的布局问题无关。 与自动布局约束失败的InputAssistantItems(位于键盘上方)有关。 如果查看自动布局错误中列出的第一个自动调整大小约束,它将引用UIKeyboardAssistantBar。
有关复制方式的更多细节:
当使用的UITextField的inputAssistantItem不会包含任何自动更正建议(它是一个安全的文本字段或将autocorrectionType设置为UIAutoCorrectionTypeNo)时,它将打破自动布局约束(我只能在某些情况下在UITextFields之间切换时重现它)或辞职/开始活跃)。
这在两种情况下发生:
在这两种情况下,中心的UIButtonBarStackView都是空的(不包含自动更正建议)。
通过将前导/尾随的BarButtonGroups设置为空数组来禁用inputAssistantItem或使用启用了自动更正的文本字段时,不会出现此问题。
编辑:
只是为了详细说明,我不建议您使用上述两种替代方法来解决此问题,因为自从您引用登录名后,您很可能会在“安全文本字段”中看到它。 相反,我的建议是忽略该警告,并在您认为适当的情况下将其报告为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.