1 Preface
The BI integration in Xcode is rich in controls, but sometimes it cannot meet our needs. Today we will learn how to use XIB to customize a UIView for reuse.
2. Detailed Process
Directory View:
2.1 create a single view application iOS application project named CustomView. For example, we do not add any controls to customviewcontroller. xib.
2.2 create a CustomView. xib. The procedure is as follows:
Drag a label and a button to the interface:
2.3 modify View attributes:
Remove Autolayout:
Set Size to Freeform and background color:
2.4 set the Size attribute of View in ZYViewController. xib to None:
ZYViewController. m code:
[Plain]-(void) viewDidLoad
{
[Super viewDidLoad];
// Obtain the nib view Array
NSArray * nib = [[NSBundle mainBundle] loadNibNamed: @ "CustomView" owner: self options: nil];
// Obtain the first UIView
UIView * tmpCustomView = [nib objectAtIndex: 0];
// Obtain the Frame of the screen
CGRect tmpFrame = [[UIScreen mainScreen] bounds];
// Set the midpoint of the custom View to the midpoint of the screen
[TmpCustomView setCenter: CGPointMake (tmpFrame. size. width/2, tmpFrame. size. height/2)];
// Add a view
[Self. view addSubview: tmpCustomView];
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Obtain the nib view Array
NSArray * nib = [[NSBundle mainBundle] loadNibNamed: @ "CustomView" owner: self options: nil];
// Obtain the first UIView
UIView * tmpCustomView = [nib objectAtIndex: 0];
// Obtain the Frame of the screen
CGRect tmpFrame = [[UIScreen mainScreen] bounds];
// Set the midpoint of the custom View to the midpoint of the screen
[TmpCustomView setCenter: CGPointMake (tmpFrame. size. width/2, tmpFrame. size. height/2)];
// Add a view
[Self. view addSubview: tmpCustomView];
}
Running result: