【发布时间】:2012-11-28 10:19:26
【问题描述】:
这是我第一次尝试使用约束,所以请原谅基本问题。
我希望能够设置一系列约束,以便子视图小于视图宽度的 75% 或视图宽度。因此,如果我的 subView 的宽度大于视图宽度的 75%,则将其设置为全宽。我想这样做以确保 subView 在 iPhone 和 iPad 上显示良好。
这可能使用约束吗?我已经设置了两个约束,但是我如何告诉约束系统根据我的视图宽度选择哪一个,因为我不确定这是否可以仅使用优先级来解决?
// Ensure it's width is either the less than 3 quarters of the page width or the full page width (including the gutter margins)
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:0.75 constant:0];
[self.constraints addObject:constraint];
constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
[self.constraints addObject:constraint];
// Add the constraints to the page
[self addConstraints:self.constraints];
非常感谢任何帮助,因为它陷入了困境。
戴夫
【问题讨论】:
-
恕我直言,这对于自动布局是不可能的,这听起来像是拆分视图的折叠功能,您有什么要求?
-
嗨斯蒂芬,感谢您的回复。我的要求是在代码中创建一个适用于 iPhone 和 iPad 的单一布局。我正在使用文本和图像创建页面布局(数据保存在 PLIST 中)。对于图像几乎是全宽的某些布局,文本空间非常小,因此在这些情况下希望它对齐全宽。任何想法将不胜感激。干杯戴夫。
-
嗯,我明白了,框架应尽可能多地处理。那么我仍然认为你不应该使用自动布局来满足这个要求。自动布局可以很好地在不同条件下保持静态 UI 的形状,但如果布局取决于内容,我认为您必须回到常规代码。
-
干杯@Stephan,已经回到了老式的layoutSubViews。不过感谢cmets,很有帮助。戴夫。
标签: iphone ios nslayoutconstraint