ios - 如何在自动布局中以编程方式执行 uilabel

标签 ios objective-c autolayout uilabel constraints

我在 Xcode 6 中做一个自动布局的项目,我以编程方式添加标签,它在 4s、5、5s 中完美工作,但在 6 和 6 plus 中不起作用。谁能帮助我,我是自动布局的新手。下面是我的编码。

UIScrollView *scroll = [[UIScrollView alloc]init];
[scroll setTranslatesAutoresizingMaskIntoConstraints:NO];
scroll.contentSize=CGSizeMake(480, 600);

[centerView addSubview:scroll];

NSDictionary *scrolldic = @{@"scrollview":scroll};

NSArray *scroll_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[scrollview(480)]" options:0 metrics:Nil views:scrolldic];

NSArray *scroll_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[scrollview(480)]" options:0 metrics:Nil views:scrolldic];

[scroll addConstraints:scroll_H];
[scroll addConstraints:scroll_V];
NSArray *scroll_posH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollview]" options:0 metrics:Nil views:scrolldic];

NSArray *scroll_posV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollview]" options:0 metrics:Nil views:scrolldic];


[self.view addConstraints:scroll_posH];
[self.view addConstraints:scroll_posV];
UILabel *header = [[UILabel alloc]init];
[header setTranslatesAutoresizingMaskIntoConstraints:NO];
header.backgroundColor = [UIColor blueColor];


 [scroll addSubview:header];

NSDictionary *headerdic = @{@"header":header};

NSArray *header_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[header(150)]" options:0 metrics:Nil views:headerdic];

 NSArray *header_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[header(30)]" options:0 metrics:Nil views:headerdic];

[header addConstraints:header_H];
[header addConstraints:header_V];

 NSArray *header_posH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-80-[header]" options:0 metrics:Nil views:headerdic];

 NSArray *header_posV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[header]" options:0 metrics:Nil views:headerdic];


[self.view addConstraints:header_posH];
[self.view addConstraints:header_posV];

enter image description here enter image description here

看上图4s标签在中间是正确的,但是在6中它向左移动了一些,有什么问题可以帮助我吗?

最佳答案

要让它发挥作用,您希望布局在不同设备上的外观也很重要。标签应该始终具有 150 的宽度并居中,还是应该始终具有 80 的左右填充?

这是你必须决定的,但约束看起来像这样:

第一种情况(宽度相同且居中):

NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:header attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem: scroll attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0];
[scroll addConstraint:centerXConstraint];

第二种情况(保持设备之间的填充):

[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-leftPadding-[header]-leftPadding-|" options:0 metrics:@{ @"leftPadding": @(80) } views:@{ @"header": header }]];

如果您需要更多帮助,请告诉我。

关于ios - 如何在自动布局中以编程方式执行 uilabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29675973/

相关文章:

ios - 设置 MaxConcurrentOperationCount =1 有什么好处

objective-c - 如何将 NSString 的每个单词大写

ios - (iOS) 拖放、对齐网格、勾选合适区域

c++ - C++程序员学习Objective-C的建议

iphone - 应用内购买,多个具有不同持续时间的自动续订订阅

ios - Firebase Flutter 使用 Apple 登录导致应用程序崩溃

ios - 如何在 Swift 中从同一站点重新加载新的 JSON 数据?

ios - 带有 AutoLayout 的 UIScrollView 在 iOS 6 中完美运行,但在 iOS 7 中却不行

ios - 尝试在标签上设置自动约束,但在长文本上失败

ios - 如何使用 Autolayout 为通用应用程序调整 UIView 的大小