在将子视图添加到 UIView 后,我可以为 UIView 设置框架吗?
我需要具有动态大小的 UIView(它可以包含 1 或 2 个标签),我做了这样的事情:
float nextY = 0.0f;
float labelHeight = 20.0f;
UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = @"Test 1";
[tempView addSubview:label1];
nextY += labelHeight;
if (something == YES)
{
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = @"Test 2";
[tempView addSubview:label2];
nextY += labelHeight;
}
tempView.frame = CGRectMake(0.0f, 0.0f, 100.0.f, nextY);
我可以吗?
I need UIView with dynamic size (it can containg 1 or 2 labels) and I do something like this:
float nextY = 0.0f;
float labelHeight = 20.0f;
UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = @"Test 1";
[tempView addSubview:label1];
nextY += labelHeight;
if (something == YES)
{
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = @"Test 2";
[tempView addSubview:label2];
nextY += labelHeight;
}
tempView.frame = CGRectMake(0.0f, 0.0f, 100.0.f, nextY);
I that ok?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正在做的事情是有效的。如果您在子视图上设置了自动调整大小蒙版,那么当您更改超级视图的框架时,系统也会调整子视图的框架。
What you are doing is valid. If you set the autoresizing mask on the subviews, then when you change the superview's frame, the system will also adjust the frames of the subviews.