实施自动布局的意见产生编程 [英] Implementing auto layout for views generated programmatically

查看:108
本文介绍了实施自动布局的意见产生编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其意见程序生成。例如:

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

是的。我会更新到豆瓣和使用Interface Builder和故事板的未来,但我没有做IOS一年编程。

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.

随着新的iPhone 5具有不同的屏幕尺寸,应用程序只是不好看,我需要实现某种形式的自动布局。有没有办法做到这一点编程,现在的而不是使用IB?

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?

谢谢了!

推荐答案

是的,有,在NSLayoutConstraint使用两种方法

Yes there is, by using two methods in NSLayoutConstraint

-(NSArray*)constraintsWithVisualFormat:options:metrics:views:
-(NSLayoutConstraint*)constraintWithItem:attribute:relatedBy:toItem:attribute:
    multiplier:constant:

视觉格式语言都打包成一个NSString
所以,我相信你iouTableView为例。

The visual format language is all packaged up into an NSString So I'll take your iouTableView for example.

[self.view addConstraints:[NSLayoutConstraint 
    constraintsWithVisualFormat:@"|[iouTableView]|" 
    options:0 
    metrics:nil 
    views:NSDictionaryOfVariableBindings(iouTableView)]];

管道符号|再presents的上海华的优势。
在[]重present视图。
所以,我们做了有我们大呼过瘾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.

视觉格式的另一个例子:
让我们来勾你的表视图,汇总标签和汇总表垂直。

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

现在这个链接了所有三个视图上垂直他们的每一个边,NSLayoutFormatAlignAllLeft告诉所有的意见,左对齐,他们会这样做基于其他方面的限制,在这种情况下,previous约束。
在()s的用于指定的视图的大小。

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.

有更有点像不平等和优先事项以及 - 符号间隔,但<一个href=\"https://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/Introduction.html\"相对=nofollow>检查了苹果文档为

There's a bit more like inequalities and priorities as well as the "-" spacer symbol but check out the apple docs for that

编辑:更正使用constraintsWithVisualFormat的实施例中所示的方法签名

Corrected the examples to use constraintsWithVisualFormat as shown in the method signature.

这篇关于实施自动布局的意见产生编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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