init会调用initWithFrame
自定义逻辑放在initWithFrame;不要放在init;不要两个方法都放。
一:使用
// .h
#import <UIKit/UIKit.h>
@interface BookmarkMainBackView : UIView
@end
// .m
@implementation BookmarkMainBackView
CF_CUSTOMERVIEW_VIEW
- (void)bankViewInit{
}
@end
二:实现
// 自定义 UIView (小心继承导致的bankViewInit覆盖)
#ifndef CF_CUSTOMERVIEW_VIEW
#define CF_CUSTOMERVIEW_VIEW \
- (instancetype)initWithCoder:(NSCoder *)aDecoder{\
self = [super initWithCoder:aDecoder];\
if (self) {\
self.userInteractionEnabled = YES;\
[self bankViewInit];\
}\
return self;\
}\
- (instancetype)initWithFrame:(CGRect)frame{\
self = [super initWithFrame:frame];\
if (self) {\
self.userInteractionEnabled = YES;\
[self bankViewInit];\
}\
return self;\
}
#endif
网友评论