Upload the Xib file step details and load the xib details
1. Directly load the xib file without the. h. m file
NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"XibView" owner:nil options:nil];UIView *xibView = objs[0];xibView.backgroundColor = [UIColor redColor];[self.view addSubview:xibView];
UINib
// A nib object represents an xib File
// UINib * nib = [UINib nibWithNibName: @ "XibView" bundle: [NSBundle mainBundle];
// Generally, the bundle is passed to nil. The default value is mainbundle.
UINib *nib = [UINib nibWithNibName:@"XibView" bundle:nil]; NSArray *objs = [nib instantiateWithOwner:nil options:nil]; [self.view addSubview:objs[0]];
2. The. h. m file is required and connected.
Method 1. Use Only One xib
Steps:
1. Create a new view that inherits the UIView name: XibView
2. Create an xib file named XibView.
3. In the xib File, click view (not set File's owner), and set Class on the Right To XibView.
// You can drag the control in xib to. h In XibView.
4. Use this view externally
NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"XibView" owner:nil options:nil]; XibView *view = objs.firstObject; [self.view addSubview:view];
Method 2: Use another xib OtherView file in one xib
1. Same as OtherView
2. Same as OtherView
3. In the xib File, click File owner and select OtherView as the Class on the right.
4. Drag the xib view to the. h file of the OtherView.
5. The used OtherView needs to be initialized.
// InitWithCoder is the entry to the xib File
- (instancetype)initWithCoder:(NSCoder *)coder{ self = [super initWithCoder:coder]; if (self) { [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; [self addSubview:self.view]; } return self;}- (void)layoutSubviews { [super layoutSubviews]; self.view.frame = self.bounds;}