【发布时间】:2013-06-13 14:16:52
【问题描述】:
我有一个带有 12 个UITextFields 的视图控制器。
它非常适合 3.5" 显示器。
我需要为 iPhone 5(4 英寸显示屏)设置它,以便所有 UITextFields 覆盖整个 UIView,方法是在它们之间添加额外的空间。
我正在尝试通过自动布局来做到这一点,但它无法正常工作。
这是我的代码:
- (void) viewWillLayoutSubviews
{
int h = txt1.bounds.size.height * 12;
float unusedHorizontalSpace = self.view.bounds.size.height - h ;
NSNumber* spaceBetweenEachButton= [NSNumber numberWithFloat: unusedHorizontalSpace / 13 ] ;
NSMutableArray *constraintsForButtons = [[NSMutableArray alloc] init];
[constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-50-[txt1(==30)]-(space)-[txt2(==txt1)]-(space)-[txt3(==txt1)]-(space)-[txt4(==txt1)]-(space)-[txt5(==txt1)]-(space)-[txt6(==txt1)]-(space)-[txt7(==txt1)]-(space)-[txt8(==txt1)]-(space)-[txt9(==txt1)]-(space)-[txt10(==txt1)]-(space)-[txt11(==txt1)]-(space)-[txt12]-(space)-|"
options: NSLayoutFormatAlignAllCenterX
metrics: @{@"space":spaceBetweenEachButton}
views: NSDictionaryOfVariableBindings(txt1,txt10,txt11,txt12,txt2,txt3,txt4,txt5,txt6, txt7,txt8,txt9)]];
[self.view addConstraints:constraintsForButtons];
}
如果我使用[txt12(==txt1)],那么它会显示与 3.5" 屏幕相同的内容,并在下方留出空间。
我哪里出错了?
【问题讨论】:
-
在您的
constraintsWithVisualFormat字符串中,您的第一个“空格”是一个常量 50,并且您只使用了 12 个“空格”标识符,即使您计算了 13。将50更改为(space)或将spaceBetweenEachButton计算为unusedHorizontalSpace/12 - 50。不确定它是否会起作用,但值得一试。 -
您实际上从上面的代码中得到了什么?
-
@Hari Karam Sing 查看第二张图片。
-
哦,我明白了。你的间距是计算出来的,然后作为一个固定值传递,不是吗?您是否检查过该值是否正确计算为 Retina4 尺寸较大?
-
我有一个很好的解决方案,它不使用垫片并使用 IB。 [这里][1] [1]:stackoverflow.com/a/25898949/951349
标签: ios autolayout