使用XIB自定義一個UIView,然后將這個view添加到controller的view 上(相當於所有界面都通過xib來實現)


一、新建一個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];

   

}

運行,最后效果如下所示:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM