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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章