当前位置:首页 > 操作系统 > Ios

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


原文:http://blog.csdn.net/wusoule/article/details/45624469


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!