iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
一、新建一个single view application类型的iOS application工程,名字取为CustomView,如下图,我们不往CustomViewViewController.xib中添加任何控件:
![](http://hi.csdn.net/attachment/201202/14/0_1329230542Q7Eh.gif)
二、新建一个CustomView.xib,过程如下:
![](http://hi.csdn.net/attachment/201202/14/0_1329230692ON6U.gif)
![](http://hi.csdn.net/attachment/201202/14/0_13292307112G3H.gif)
![](http://hi.csdn.net/attachment/201202/14/0_13292311729Bk3.gif)
![](http://hi.csdn.net/attachment/201202/14/0_1329230542Q7Eh.gif)
二、新建一个CustomView.xib,过程如下:
![](http://hi.csdn.net/attachment/201202/14/0_1329230692ON6U.gif)
![](http://hi.csdn.net/attachment/201202/14/0_13292307112G3H.gif)
然后往界面上拖一个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];
}
运行,最后效果如下所示:![](http://hi.csdn.net/attachment/201202/14/0_13292311729Bk3.gif)