Home > Blockchain >  Objective C create UIView programatically should work in both portrait and landscape
Objective C create UIView programatically should work in both portrait and landscape

Time:09-29

I've to create a view in objective c with two labels on it, label1 has single line and label2 is multi line based on content.

Based on content in the labels I want to adjust view height how can I do that?

Width of the view should be 20 left and right to screen width with below code I can show in portrait but in landscape it was not coming properly, It crops on the right side. How can I do show that right 20 for landscape?

Portrait

enter image description here

Landscape

enter image description here

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

Am I doing anything wrong?

CodePudding user response:

A few observations:

  1. Do not add subviews in viewDidLayoutSubviews. That is only for adjusting frame values.

  2. If you refer to self.view, reference its bounds, not its frame. The former is for coordinates of subviews within self.view. The latter is in the coordinate system of its superview (which might not introduce any differences right now, but is just the wrong coordinate system.

  3. If you want the view to resize on rotation, set its autoresizingMask, e.g.

    emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    

    Or

    emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    
  4. Nowadays, setting frame values and autoresizing masks is a bit of an anachronism. We would generally use constraints, e.g.

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

    Note the translatesAutoresizingMaskIntoConstraints is false and the constraints.

  • Related