Question

I have a problem with a custom view and autolayout. To make things simple I will use two UILabels, the first one should change its background color when the device rotate. The problem is that it doesn't do it! Any hint? Thanks! Nicola

- (id)init
{
    self = [super init];
    if (self) {

        //Add the subviews to the mainView
        [self.view addSubview:self.label1];
        [self.view addSubview:self.label2];

        //Autolayout

        //Create the views dictionary
        NSDictionary *viewsDictionary = @{@"header":self.label1,
                                          @"table": self.label2};

        //Create the constraints using the visual language format   

        [self.view addConstraints:[NSLayoutConstraint
            constraintsWithVisualFormat: @"H:|[header]|"
                              options:0
                              metrics:nil
                                views:viewsDictionary]];

        [self.view addConstraints:[NSLayoutConstraint
            constraintsWithVisualFormat: @"H:|[table]|"
                              options:0
                              metrics:nil
                                views:viewsDictionary]];

        [self.view addConstraints:[NSLayoutConstraint
            constraintsWithVisualFormat:@"V:|[header(==50)][table]|"
                              options:0
                              metrics:nil
                                views:viewsDictionary]];

    }
    return self;
}


-(UIView*) label1
{
    _label1 = [UILabel alloc] init];

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
        _label1.backgroundColor = [UIColor redColor];
    }else{
        _label1.backgroundColor = [UIColor greenColor];
    }

    _label1.translatesAutoresizingMaskIntoConstraints=NO;
    return _label1;
}

-(UIView*) label2
{
    _label2 = [UILabel alloc] init];
    _label2.backgroundColor = [UIColor yellowColor];
    _label2.translatesAutoresizingMaskIntoConstraints=NO;
    _return label2;
}

-(BOOL) shouldAutorotate
{
    return YES;
}

-(NSUInteger) supportedInterfaceOrientations
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        //I am on a pad
        return UIInterfaceOrientationMaskAll;
    } else {
        //I am on a Phone
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //I expect the label1 to change its background color
    [self.view setNeedDisplay];
}
Was it helpful?

Solution

If you move the code related to [self.label1 setBackgroundColor:] to the delegate method didRotateFromInterfaceOrientation:, it should work better. Also, in your custom getters, you are allocating a new label every time you access the method. In most situations it's preferable to check if the ivar is not nil at the beginning, and returning the ivar, instead for allocating a fresh label.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top