[英]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.