iPhone開發學習筆記005——使用XIB自定義一個UIView,然後將這個view新增到controller的view
阿新 • • 發佈:2019-01-04
二、新建一個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];
}
執行,最後效果如下所示: