1. xib
的view
.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface AView : UIView
@property (strong, nonatomic) IBOutlet UIView *vContent;
@property (strong, nonatomic) IBOutlet UILabel *lbName;
@end
NS_ASSUME_NONNULL_END
.m
#import "AView.h"
@implementation AView
/** 要点:此view的xib中 custom class不设置 File's Owner设置为 AView */
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self addSubView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubView];
}
return self;
}
/*
(lldb) po self.vContent
<UIView: 0x104f1b060; frame = (0 0; 253 169); autoresize = RM+BM; layer = <CALayer: 0x28241d380>>
(lldb) po containerView
<UIView: 0x104f1b060; frame = (0 0; 253 169); autoresize = RM+BM; layer = <CALayer: 0x28241d380>>
*/
- (void)addSubView {
UIView *containerView = [[[UINib nibWithNibName:@"AView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
CGRect newFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
containerView.frame = newFrame;
containerView.backgroundColor = [UIColor cyanColor];
[self addSubview:containerView];
}
@end
2. xib
要点:
xib中 custom class 不设置
File's Owner 的 class 设置为 AView ,之后控件可以拖线
网友评论