NSLayoutConstraint并确定哪个子视图更宽

时间:2015-04-23 17:33:24

标签: ios objective-c autolayout nslayoutconstraint

我在子容器UISwitch中创建了UILabelUIView

UISwitch *toggleSwitch = [UISwitch new];
toggleSwitch.translatesAutoresizingMaskIntoConstraints = FALSE;
[toggleSwitch addTarget:self action:@selector(switchToggleDetected:) forControlEvents:UIControlEventValueChanged];
self.toggleSwitch = toggleSwitch;

[self addSubview:toggleSwitch];

UILabel *label = [UILabel new];
label.translatesAutoresizingMaskIntoConstraints = FALSE;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:10.0];
label.text = [self.text uppercaseString];

self.label = label;

[self addSubview:label];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[toggleSwitch]-5-[label]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(toggleSwitch, label)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[toggleSwitch]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(toggleSwitch, label)]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

给了我这个:

enter image description here

最后两个约束让我给容器视图一个"内在大小"。容器是rect(0,0,0,0)。然后我告诉右侧应该与UILabel的宽度相同,并且底部应该与UILabel的值相同以给出高度。

我可能遇到的问题是标签比交换机短:

enter image description here

这将导致交换机容器视图放置不正确的问题:

[contentImageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[toggleView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(toggleView)]];

enter image description here

所以我想将约束基于两者中的较长者,无论是开关还是标签,但我不确定如何确定哪个更宽。这个开关是一个恒定的宽度,但是我不能得到标签的宽度,直到将它添加到屏幕上为时已晚。

我已尝试添加[self layoutIfNeeded][label layoutIfNeeded]

[self layoutIfNeeded];
[label layoutIfNeeded];

DLog(@"label: %@", label);
DLog(@"switch: %@", toggleSwitch);

//Constraints added here

结果:

DEBUG | -[SwitchContainerView createContainerSwitch] | label: <UILabel: 0x7af7c6d0; frame = (0 0; 0 0); text = 'ON'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7af7c7b0>>
DEBUG | -[SwitchContainerView createContainerSwitch] | switch: <UISwitch: 0x7af7b370; frame = (0 0; 51 31); layer = <CALayer: 0x7af7b430>>

有关确定两者中哪一个更宽的建议?

1 个答案:

答案 0 :(得分:0)

不需要来确定哪个更广泛&#34; - 只需设置约束并离开,让自动布局完成其工作。只需使用两个带有不等式/优先级的约束来确定包含的超级视图相对于两个子视图的宽度。

我仅使用约束来实现这种安排 - 我现在所做的只是改变标签的文字:

enter image description here

enter image description here

每当标签文字很短时,超级视图比开关宽20个点。每当标签文本很长时,超级视图比标签宽20个点。这是完全使用约束配置 。以下是在我的屏幕截图中确定黄色超级视图宽度的约束(如调试器控制台中所示):

UIView:0x7fdb03435450.width >= UISwitch:0x7fdb034355c0.width + 20 priority:999
UIView:0x7fdb03435450.width >= UILabel:0x7fdb03433260'Infundibulum'.width + 20 priority:999

约束不会改变;我将它们设置一次并且布局正常工作,并随着标签文本的变化而继续工作。

相关问题