ios 为以编程方式生成的视图实现自动布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12593153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing auto layout for views generated programmatically
提问by okysabeni
I have an app whose views are generated programmatically. Example:
我有一个应用程序,其视图以编程方式生成。例子:
-(void)loadView
{
[super loadView];
// SET TOP LEFT BTN FOR NEXT VIEW
UIBarButtonItem *topLeftBtn = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = topLeftBtn;
[topLeftBtn release];
// programmatically set up the view for cart tableView
CGRect iouTableViewFrame = CGRectMake(0, 0, 320, 348);
iouTableView = [[UITableView alloc]initWithFrame:iouTableViewFrame style:UITableViewStylePlain];
[[self iouTableView] setDelegate:self];
[[self iouTableView] setDataSource:self];
[[self view] addSubview:iouTableView];
// set up the summary label
CGRect summaryTableFrame = CGRectMake(0, 348, 320, 18);
UILabel *summaryTableLabel = [[UILabel alloc] initWithFrame:summaryTableFrame];
[summaryTableLabel setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[summaryTableLabel setText:@" Summary"];
UIColor *labelColor = UIColorFromRGB(MiddleBlueColor);
[summaryTableLabel setBackgroundColor:labelColor];
[summaryTableLabel setTextColor:[UIColor whiteColor]];
[[self view] addSubview:summaryTableLabel];
// set up the summary table
CGRect summaryTableViewFrame = CGRectMake(0, 366, 320, 44);
summaryTableView = [[UITableView alloc]initWithFrame:summaryTableViewFrame style:UITableViewStylePlain];
[summaryTableView setScrollEnabled:NO];
[[self summaryTableView] setDelegate:self];
[[self summaryTableView] setDataSource:self];
[[self view] addSubview:summaryTableView];
}
Yes. I will update to NIBs and use interface builder and storyboard in the future but I have not done ios programming in a year.
是的。以后我会更新到NIBs,使用interface builder和storyboard,但我已经有一年没有做过ios编程了。
With the new iPhone 5 having a different screen size, the app just does not look good and I need to implement auto layout of some sort. Is there a way to do it programmatically for now instead of using IB?
由于新 iPhone 5 的屏幕尺寸不同,该应用程序看起来不太好,我需要实现某种自动布局。现在有没有办法以编程方式而不是使用 IB?
Thanks much!
非常感谢!
回答by David Wong
Yes there is, by using two methods in NSLayoutConstraint
是的,通过在 NSLayoutConstraint 中使用两种方法
-(NSArray*)constraintsWithVisualFormat:options:metrics:views:
-(NSLayoutConstraint*)constraintWithItem:attribute:relatedBy:toItem:attribute:
multiplier:constant:
The visual format language is all packaged up into an NSString So I'll take your iouTableView for example.
视觉格式的语言都打包成一个NSString所以我相信你iouTableView例如。
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"|[iouTableView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(iouTableView)]];
The pipe symbol "|" represents the superview's edge. The [] represent a view. So what we did there was we hooked the iouTableView's left and right edge to the left and right edge of its superview.
管道符号“|” 代表父视图的边缘。[] 代表一个视图。所以我们在那里做的是将 iouTableView 的左右边缘挂钩到其超级视图的左右边缘。
Another example of the visual format: Let's hook your table view, summary label and summary table vertically.
视觉格式的另一个示例:让我们垂直挂钩您的表视图、摘要标签和摘要表。
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|[iouTableView(348)][summaryTableLabel(18)][summaryTableView(44)]"
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:NSDictionaryOfVariableBindings(iouTableView, summaryTableLabel, summaryTableView)]];
Now this links up all three views vertically on each of their edges, NSLayoutFormatAlignAllLeft tells all the views to align left and they'll do so based on other constraints, in this case, the previous constraint. The ()s are used to specify the size of the views.
现在这将所有三个视图在它们的每条边上垂直连接起来, NSLayoutFormatAlignAllLeft 告诉所有视图向左对齐,它们将根据其他约束(在本例中为前一个约束)这样做。() 用于指定视图的大小。
There's a bit more like inequalities and priorities as well as the "-" spacer symbol but check out the apple docs for that
有点像不平等和优先级以及“-”间隔符号,但请查看苹果文档
Edit: Corrected the examples to use constraintsWithVisualFormat as shown in the method signature.
编辑:更正了示例以使用constraintsWithVisualFormat,如方法签名中所示。
回答by DAloG
In addition to Aplle provided methods you can use Paruslib for operating with AutoLayout from code.
除了 Aplle 提供的方法之外,您还可以使用Paruslib 从代码中操作 AutoLayout。
For example you will be able to specify:
例如,您将能够指定:
PVVFL(@"[view1]-20-[view2]").fromRightToLeft.withViews(views).asArray
Instead of
代替
[NSLayoutConstraint constraintsWithVisualFormat:@"[view1]-20-[view2]"
options:NSLayoutFormatDirectionRightToLeft
metrics:nil
views:views]
Also you will be able to group layouts settings, mix VFL and not VFL constraints. Parusable to prevent common mistakes, differentiate locationand parametersconstriaints, and provide great auto-completion support.
您还可以对布局设置进行分组,混合 VFL 和非 VFL 约束。 Parus能够防止常见错误,区分位置和参数约束,并提供强大的自动完成支持。