ios 載入xib遇到的坑

來源:互聯網
上載者:User

ios 載入xib遇到的坑

storyboard,個人覺得是個好玩意兒,但是什麼都做到其中總覺得雜亂。個人偏好把複雜的局部控制項(比如定製的collectionviewcell)在xib檔案中拉好。

在開發過程中遇到不少坑,記憶猶新的是:xib中的組件(比如button)設定圓角的效果不對;載入的xib不能resize大小。

第一個問題:圓角的設定代碼:

 

view.layer.cornerRadius = view.frame.size.height / 2;

 

使用了autolayout則需要注意調用的地方:(因為autolayout會重新計算frame,在這裡調用子控制項的frame才是正確的)

 

 

- (void)viewDidLayoutSubviews;

 

好了,如果xib呢?在下面函數中,寫你需要達到的子控制項的效果。

 

 

-layoutSubviews;

 

在xib的擁有者調用以下函數,下面函數會出發layoutSubviews

 

 

[xib layoutIfNeeded]

-----------華麗的分割線----------------

 

第二個問題:你設定了xib的大小,但是介面上xib還是頑固的顯示它的xib中的大小。腫麼辦。

 

- (void)awakeFromNib {    [super awakeFromNib];    //solve UICollectionViewCell subviews do not resize    self.contentView.autoresizingMask =        //UIViewAutoresizingFlexibleLeftMargin |        UIViewAutoresizingFlexibleWidth |        //UIViewAutoresizingFlexibleRightMargin |        //UIViewAutoresizingFlexibleTopMargin |        UIViewAutoresizingFlexibleHeight        //UIViewAutoresizingFlexibleBottomMargin    ;    self.contentView.translatesAutoresizingMaskIntoConstraints = YES;}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.