提问人:iOSDude 提问时间:9/28/2021 最后编辑:pkambiOSDude 更新时间:9/30/2021 访问量:532
使用 Objective-C 以编程方式创建 UIView,以纵向和横向工作
Create UIView programmatically with Objective-C to work in both portrait and landscape
问:
我必须在目标 c 中创建一个包含两个标签的视图,label1 具有单行,label2 是基于内容的多行。
根据标签中的内容,我想调整视图高度,我该怎么做?
视图的宽度应该是 20 左右到屏幕宽度,使用以下代码我可以纵向显示,但在横向中它没有正确出现,它在右侧裁剪。我怎样才能为风景显示正确的 20?
肖像
景观
UIView *emptyTreeView = [[UIView alloc] initWithFrame:CGRectMake(20,20,self.view.frame.size.width - 40,400)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 30.0, self.view.frame.size.width - 100, 30.0)];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 90.0, self.view.frame.size.width - 100, 100.0)];
emptyTreeView.backgroundColor=[UIColor blueColor];
label1.backgroundColor = [UIColor redColor];
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor blackColor];
label1.font = [UIFont boldSystemFontOfSize:18];
label1.numberOfLines = 0;
label1.text = @"The Page Cannot be displayed.";
label2.backgroundColor = [UIColor whiteColor];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor grayColor];
label2.font = [UIFont systemFontOfSize:15];
label2.numberOfLines = 0;
label2.text = @"Please use this feature or contact your internal contact person directly.";
[emptyTreeView addSubview:label1];
[emptyTreeView addSubview:label2];
[self.view addSubview:emptyTreeView];
我做错了什么吗?
答:
2赞
Rob
9/28/2021
#1
几点观察:
不要在 中添加子视图。这仅用于调整值。
viewDidLayoutSubviews
frame
如果引用 ,请引用 其 ,而不是其 。前者用于 内的子视图的坐标。后者位于其上视图的坐标系中(目前可能不会引入任何差异,但只是错误的坐标系。
self.view
bounds
frame
self.view
如果您希望视图在旋转时调整大小,请将其设置为 ,例如
autoresizingMask
emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
或
emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
如今,设置帧值和自动调整蒙版大小有点不合时宜。我们通常会使用约束,例如
- (void)viewDidLoad { [super viewDidLoad]; UIView *emptyTreeView = [[UIView alloc] init]; UILabel *label1 = [[UILabel alloc] init]; UILabel *label2 = [[UILabel alloc] init]; emptyTreeView.backgroundColor = [UIColor blueColor]; label1.backgroundColor = [UIColor redColor]; label1.textAlignment = NSTextAlignmentCenter; label1.textColor = [UIColor blackColor]; label1.font = [UIFont boldSystemFontOfSize:18]; label1.numberOfLines = 0; label1.text = @"The Page Cannot be displayed."; label2.backgroundColor = [UIColor whiteColor]; label2.textAlignment = NSTextAlignmentCenter; label2.textColor = [UIColor grayColor]; label2.font = [UIFont systemFontOfSize:15]; label2.numberOfLines = 0; label2.text = @"Please use this feature or contact your internal contact person directly."; [emptyTreeView addSubview:label1]; [emptyTreeView addSubview:label2]; [self.view addSubview:emptyTreeView]; emptyTreeView.translatesAutoresizingMaskIntoConstraints = false; label1.translatesAutoresizingMaskIntoConstraints = false; label2.translatesAutoresizingMaskIntoConstraints = false; [NSLayoutConstraint activateConstraints:@[ [emptyTreeView.leftAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leftAnchor constant:20], [emptyTreeView.rightAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.rightAnchor constant:-10], [emptyTreeView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20], [label1.leftAnchor constraintEqualToAnchor:emptyTreeView.leftAnchor constant:20], [label1.rightAnchor constraintEqualToAnchor:emptyTreeView.rightAnchor constant:-20], [label1.topAnchor constraintEqualToAnchor:emptyTreeView.topAnchor constant:20], [label2.leftAnchor constraintEqualToAnchor:emptyTreeView.leftAnchor constant:20], [label2.rightAnchor constraintEqualToAnchor:emptyTreeView.rightAnchor constant:-20], [label2.topAnchor constraintEqualToAnchor:label1.bottomAnchor constant:20], [emptyTreeView.bottomAnchor constraintEqualToAnchor:label2.bottomAnchor constant:20] ]]; }
请注意 is 和约束。
translatesAutoresizingMaskIntoConstraints
false
评论
1赞
iOSDude
9/28/2021
非常感谢它按预期工作。
评论