一、新建一個single view application類型的iOS application工程,名字取為CustomView,如下圖,我們不往CustomViewViewController.xib中添加任何控件:
二、新建一個CustomView.xib,過程如下:
然后往界面上拖一個label和一個button:
接下來得建立CustomViewViewController.xib中的view與CustomView.xib的關聯了,即將Custom.xib動態加載,然后將該view做為CustomViewViewController的view的subview,放置於正中顯示(設置CustomView的中心點為整屏正中央),具體代碼如下:
- (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *nib = [[NSBundlemainBundle]loadNibNamed:@"CustomView"owner:selfoptions:nil]; UIView *tmpCustomView = [nibobjectAtIndex:0]; CGRect tmpFrame = [[UIScreenmainScreen] bounds]; [tmpCustomView setCenter:CGPointMake(tmpFrame.size.width / 2, tmpFrame.size.height / 2)]; [self.viewaddSubview:tmpCustomView]; }
運行,最后效果如下所示: