NSLayoutConstraint和确定哪些子视图更宽 [英] NSLayoutConstraint and determining which subview is wider

查看:704
本文介绍了NSLayoutConstraint和确定哪些子视图更宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 UISwitch 的UILabel 在子类容器的UIView

I create a UISwitch and a UILabel in a subclassed container UIView:

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]];

这给了我这样的:

which gives me this:

最后两个约束让我给容器查看固有大小。该容器是矩形(0,0,0,0)。那么我告诉右边应该是相同的宽度的UILabel 键,底部应该是相同的值的UILabel 给予的高度。

The last two constraints lets me give the container view an "intrinsic size". The container would be rect(0,0,0,0). I then tell the right side should be the same width as the UILabel and the bottom should be the same value as the UILabel to give a height.

我可能运行的问题是当标签是短则开关

The problem I may run is when the label is shorter then the switch:

这将导致与交换机容器视图的问题没有被放在正确的:

This will lead to an issue with the switch container view not being placed correct:

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

所以,我想在基础上的限制两人的时间越长,交换机或标签,但我不知道如何确定哪个是更广泛的。该开关是恒定的宽度,但直到后加入到屏幕,这将是太晚我不能得到的标签的宽度

So I would like to base the constraints on the longer of the two, either the switch or the label but I'm not sure how to determine which is wider. The switch is a constant width, but I can not get the width of the label until after is added to screen which would be too late.

我试着加入 [自layoutIfNeeded] [标签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>>

在确定哪些两个较宽?任何建议

Any suggestions on determining which of the two is wider?

推荐答案

有不需要的的以确定哪个是更广泛的 - 刚刚成立的约束走开,让汽车布局完成其工作。只需使用带有不平等/优先级两个约束条件,以确定上海华相对于含有的宽度与两个子视图。

There is no need for you to "determine which is wider" - just set up the constraints and walk away, and let auto layout do its job. Simply use two constraints with inequalities / priorities to determine the width of the containing superview relative to the two subviews.

我实现了使用约束独此安排 - 所有的,我在这一点上做的是改变标签的文本:

I achieved this arrangement using constraints alone - all I'm doing at this point is changing the text of the label:

每当标签文本短,上海华比开关更宽20分。每当标签文本长,上海华比标签更广泛的20分。在配置的完全有约束的。下面是确定在我的屏幕截图黄色上海华的宽度(如在调试器控制台中显示)的限制:

Whenever the label text is short, the superview is 20 points wider than the switch. Whenever the label text is long, the superview is 20 points wider than the label. That is configured entirely with constraints. Here are the constraints that determine the width of the yellow superview in my screen shots (as shown in the debugger console):

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

约束不改变;我设置它们一次,布局只是工作,保持工作作为标签的文本更改。

The constraints do not change; I set them up once and the layout just works, and keeps working as the text of the label changes.

这篇关于NSLayoutConstraint和确定哪些子视图更宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆