gpt4 book ai didi

ios - 为以编程方式生成的 View 实现自动布局

转载 作者:技术小花猫 更新时间:2023-10-29 11:10:20 26 4
gpt4 key购买 nike

我有一个应用程序,其 View 是通过编程方式生成的。示例:

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

是的。我将更新到 NIB,并在未来使用界面构建器和 Storyboard,但我已经一年没有做过 ios 编程了。

新的 iPhone 5 有不同的屏幕尺寸,应用程序看起来不太好,我需要实现某种自动布局。现在有没有办法以编程方式代替使用 IB?

非常感谢!

最佳答案

是的,通过在 NSLayoutConstraint 中使用两种方法

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

视觉格式语言全部打包成一个NSString因此,我将以您的 iouTableView 为例。

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

管道符号“|”代表 super View 的边缘。[] 代表一个 View 。所以我们所做的是将 iouTableView 的左右边缘连接到它的父 View 的左右边缘。

视觉格式的另一个例子:让我们垂直 Hook 您的 TableView 、摘要标签和摘要表。

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|[iouTableView(348)][summaryTableLabel(18)][summaryTableView(44)]"
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:NSDictionaryOfVariableBindings(iouTableView, summaryTableLabel, summaryTableView)]];

现在,这三个 View 在它们的每条边上垂直链接起来,NSLayoutFormatAlignAllLeft 告诉所有 View 左对齐,它们将根据其他约束(在本例中为先前的约束)这样做。() 用于指定 View 的大小。

有一点更像是不平等和优先级以及“-”间隔符号,但 check out the apple docs for that

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

关于ios - 为以编程方式生成的 View 实现自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com