使用 Objective-C 以编程方式创建 UIView,以在纵向和横向中工作

Create UIView programmatically with Objective-C to work in both portrait and landscape

我必须在 objective 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];

我做错了什么吗?

几点观察:

  1. 不要在 viewDidLayoutSubviews 中添加子视图。这仅用于调整 frame 值。

  2. 如果您引用 self.view,请引用它的 bounds,而不是它的 frame。前者用于 self.view 内的子视图坐标。后者在其父视图的坐标系中(现在可能不会引入任何差异,但只是错误的坐标系。

  3. 如果您希望视图在旋转时调整大小,请设置其 autoresizingMask,例如

    emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    

    emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    
  4. 现在,设置帧值和自动调整蒙版大小有点不合时宜了。我们通常会使用约束,例如

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

    请注意 translatesAutoresizingMaskIntoConstraintsfalse 和约束条件。