gpt4 book ai didi

ios - 自动布局约束冲突

转载 作者:可可西里 更新时间:2023-11-01 06:20:42 25 4
gpt4 key购买 nike

尝试创建一个包含 UITextFieldUILabel 的 View 。以下代码有什么问题?

- (UIView *)tableHeaderView{
NSArray *constraints;
UIView *view = [[UIView alloc]initWithFrame:(CGRect){0,0,self.view.frame.size.width, 88}];

UITextField *tf = [[UITextField alloc]init];
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.text = _filter.name;
[view addSubview:tf];

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Found results: %d", nil), _filter.resultsCount];
[view addSubview:titleLabel];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[titleLabel]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)];
[view addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[tf]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tf)];
[view addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[titleLabel]-8-[tf]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(titleLabel, tf)];
[view addConstraints:constraints];
}

错误信息:

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)
(
"<NSLayoutConstraint:0x15dd2fc0 V:|-(0)-[UILabel:0x15dd1e10] (Names: '|':UIView:0x15dc4f90 )>",
"<NSLayoutConstraint:0x15dd3120 V:[UILabel:0x15dd1e10]-(8)-[UITextField:0x15dbe780]>",
"<NSAutoresizingMaskLayoutConstraint:0x15de2300 h=--& v=--& UITextField:0x15dbe780.midY ==>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x15dd3120 V:[UILabel:0x15dd1e10]-(8)-[UITextField:0x15dbe780]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-07-14 14:01:30.216 DossierPolice[4724:60b] 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)
(
"<NSLayoutConstraint:0x15dd2e20 H:[UITextField:0x15dbe780]-(NSSpace(20))-| (Names: '|':UIView:0x15dc4f90 )>",
"<NSAutoresizingMaskLayoutConstraint:0x15de1ee0 h=--& v=--& UITextField:0x15dbe780.midX ==>",
"<NSAutoresizingMaskLayoutConstraint:0x15de22d0 h=--& v=--& H:[UITextField:0x15dbe780(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x13a9eda0 h=--& v=--& H:[UIView:0x15dc4f90(320)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x15dd2e20 H:[UITextField:0x15dbe780]-(NSSpace(20))-| (Names: '|':UIView:0x15dc4f90 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-07-14 14:01:30.217 DossierPolice[4724:60b] 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)
(
"<NSLayoutConstraint:0x15dd3160 V:[UITextField:0x15dbe780]-(0)-| (Names: '|':UIView:0x15dc4f90 )>",
"<NSAutoresizingMaskLayoutConstraint:0x15de2300 h=--& v=--& UITextField:0x15dbe780.midY ==>",
"<NSAutoresizingMaskLayoutConstraint:0x15de2330 h=--& v=--& V:[UITextField:0x15dbe780(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x13a9ee00 h=--& v=--& V:[UIView:0x15dc4f90(88)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x15dd3160 V:[UITextField:0x15dbe780]-(0)-| (Names: '|':UIView:0x15dc4f90 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

最佳答案

您忘记在 tf 上将 translatesAutoresizingMaskIntoConstraints 设置为 NO。此属性阻止布局引擎自动将旧样式自动调整大小掩码转换为约束,这就是错误的原因。

关于ios - 自动布局约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24734069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com