Search code examples
iphoneiosuikitcore-animation

UIView reflect CALayer properties


When changing a UIView's basic view properties (eg backgroundColor, alpha/opacity, frame) is it actually the layer that get's those changes under the hood? To put it simple, when I change the UIViews alpha for example is it actually the CALayers opacity property that changes?


Solution

  • On iOS, views are fairly thin wrappers around layers, and most view properties like the frame/bounds, alpha, etc. are indeed just manipulating the underlying layer. You can verify your alpha example with a little snippet of code in a view controller:

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        self.view.alpha = 0.5f;
        NSLog(@"Layer alpha: %f", self.view.layer.opacity);
    
        self.view.layer.opacity = 1.0f;
        NSLog(@"View alpha: %f", self.view.alpha);
    }
    

    This prints out:

    Layer alpha: 0.500000
    View alpha: 1.000000
    

    So we can see that the view alpha and layer opacity are the same thing.