- (UIImageView *)roundedRectImageViewWithCornerRadius:(CGFloat)cornerRadius {
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];
//UIBezierPath * bezierPath = [UIBezierPath bezierPathWithRoundedRect:backView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerTopLeft cornerRadii:CGSizeMake(width, height)];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = bezierPath.CGPath;
self.layer.mask = layer;
return self;
}
UIButton
說明:UIButton 的背景圖片,如果是復雜的圖片,可以依靠 UI 切圖來實現。如果是簡單的純色背景圖片,可以利用代碼繪制帶圓角的圖片。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 設置 UIButton 的背景圖片。
[button setBackgroundImage:image forState:UIControlStateNormal];
背景圖片繪制方法
+ (UIImage *)pureColorImageWithSize:(CGSize)size color:(UIColor *)color cornRadius:(CGFloat)cornRadius {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height)];
view.backgroundColor = color;
view.layer.cornerRadius = cornerRadius;
// 下面方法,第一個參數表示區域大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數是屏幕密度
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}