接下來得建立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];
}
運行,最後效果如下所示:
Demo下載地址:http://up.2cto.com/2012/0225/20120225115338881.rar
摘自 Code Heaven