OC中
自定义控件的.h文件
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface MyImageView : UIImageView
@property (nonatomic) IBInspectable CGFloat cornerRadius;
@end
自定义控件的.m文件
#import "MyImageView.h"
@implementation MyImageView
- (void)setCornerRadius:(CGFloat)cornerRadius
{
_cornerRadius = cornerRadius;
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = cornerRadius > 0;
}
@end
Swift中
import UIKit
@IBDesignable
class MyImageView: UIImageView {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.layer.masksToBounds = cornerRadius > 0
}
}
}
网友评论