UIView with rounded corners and drop shadow?

前端 未结 30 3097
一整个雨季
一整个雨季 2020-11-22 08:00

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.

30条回答
  •  借酒劲吻你
    2020-11-22 08:17

    I solved the problem using the following trick when assigning shadow path for the container view :

    [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:12]
    

    Notice that the path given to the shadow is a rounded rectangle with the same corner radius as the background that the cell contains:

    //this is the border for the UIView that is added to a cell
    cell.backgroundView.layer.cornerRadius = 12;
    cell.backgroundView.layer.masksToBounds = YES;
    cell.backgroundView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    cell.backgroundView.layer.borderWidth = 1;
    
    //this is the shadow around the cell itself (cannot have round corners with borders and shadow, need to use two views
    cell.layer.shadowRadius = 2;
    cell.layer.cornerRadius = 12;
    cell.layer.masksToBounds = NO;
    [[cell layer] setShadowColor:[[UIColor darkGrayColor] CGColor]];
    
    [[cell layer] setShadowOffset:CGSizeMake(0.0,0.0)];
    [[cell layer] setShadowOpacity:1.0];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:12];
    [[cell layer] setShadowPath:[path CGPath]];
    

提交回复
热议问题