@interface UIView (Add)
- (void)setCornerRadius:(CGFloat)radius withShadow:(BOOL)shadow withOpacity:(CGFloat)opacity;
@end
@implementation UIView (Add)
- (void)setCornerRadius:(CGFloat)radius withShadow:(BOOL)shadow withOpacity:(CGFloat)opacity {
self.layer.cornerRadius = radius;
if (shadow) {
self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
self.layer.shadowOpacity = opacity;
self.layer.shadowOffset = CGSizeMake(-1, 1);
self.layer.shadowRadius = radius;
self.layer.shouldRasterize = NO;
self.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:radius] CGPath];
}
self.layer.masksToBounds = !shadow;
}
@end
(void)layoutSubviews {
[super layoutSubviews];
[self setCornerRadius:2 withShadow:YES withOpacity:0.1];
}
可以实现一些在布局已定,修改小的地方。