Drew
Drew

Reputation: 1452

CALayer with different effects between iPhone / iPad

This has me a bit confused. I have a universal app which has two different Storyboards for iPhone and iPad. I'm using a small block of CALayer code to round the edges of a view and throw a shadow on it. Works perfectly on the iPhone.

[self configShadowLayer:_view
           cornerRadius:_view.frame.size.width / 2.0
          shadowOffsetX:0.0
          shadowOffsetY:3.0
           shadowRadius:2.0
                opacity:0.2];


-(void)configShadowLayer:(UIView *)shadowView
        cornerRadius:(float)cornerRadius
       shadowOffsetX:(float)shadowOffsetX
       shadowOffsetY:(float)shadowOffsetY
        shadowRadius:(float)shadowRadius
             opacity:(float)opacity {

    CALayer *shadowLayer = shadowView.layer;
    shadowLayer.masksToBounds = NO;
    shadowLayer.cornerRadius = cornerRadius;
    shadowLayer.shadowOffset = CGSizeMake(shadowOffsetX, shadowOffsetY);
    shadowLayer.shadowRadius = shadowRadius;
    shadowLayer.shadowOpacity = opacity;
}

On the iPad, it gives me a diamond shape. I have to change the corner radius from the half to a quarter and then it works. But then of course the iPhone view goes from a circle to a rounded rect.

What am I missing?

iPhone view

enter image description here

Upvotes: 1

Views: 65

Answers (1)

clemens
clemens

Reputation: 17721

You should call configShadowLayer:cornerRadius:shadowOffsetX:shadowOffsetY:shadowRadius:opacity: after the layout of the view is done. When you call it only before the layout process the view's frame has probably the wrong sizes, and you will get a corner radius which is too large.

BTW: You should compute the radius depending to the bounds of the view, because the bounds represents the coordinate system inside of the view.

Upvotes: 1

Related Questions