How to add a drop shadow to a UIButton?

后端 未结 5 1003
面向向阳花
面向向阳花 2020-12-02 07:14

I would like to add a drop shadow to a UIButton. I tried to use self.layer.shadow* properties. Those properties work in UIView, but they behave differently in UIButton. I wo

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 07:21

    There is only one tiny mistake in the question that causes the shadow to not be displayed: masksToBounds:YES also masks the shadow! Here's the correct code:

    self.layer.cornerRadius = 8.0f;
    self.layer.masksToBounds = NO;
    self.layer.borderWidth = 1.0f;
    
    self.layer.shadowColor = [UIColor greenColor].CGColor;
    self.layer.shadowOpacity = 0.8;
    self.layer.shadowRadius = 12;
    self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
    

    Unfortunately, this means we cannot mask the content and have a shadow at the same time without tricks.

    Remember to #import .

提交回复
热议问题