加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 在xib中向“主视图”添加约束

发布时间:2020-12-14 19:15:50 所属栏目:百科 来源:网络整理
导读:我在.xib文件中定义了UIView.我需要设置translatesAutoresizingMaskIntoConstraints = NO.这意味着框架没有转换为约束,所以我需要自己设置大小约束. 我为UIView创建了一个工作类别方法: -(NSArray*)setSizeConstraints:(CGSize)size{ NSLayoutConstraint* h
我在.xib文件中定义了UIView.我需要设置translatesAutoresizingMaskIntoConstraints = NO.这意味着框架没有转换为约束,所以我需要自己设置大小约束.

我为UIView创建了一个工作类别方法:

-(NSArray*)setSizeConstraints:(CGSize)size
{
    NSLayoutConstraint* height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1  constant:size.height];
    NSLayoutConstraint* width = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1  constant:size.width];

    [self addConstraint:height];
    [self addConstraint:width];

    return [NSArray arrayWithObjects:height,width,nil];
}

但我想从Xcode界面构建器设置这些约束,但我的所有AutoLayout控件都是灰色的:

有没有办法在界面构建器中执行此操作?

解决方法

这实际上是可能的.

只需将您想要约束的视图限制在另一个视图中,例如此处突出显示的视图.

enter image description here

然后,添加所需的约束.

最后,将刚添加约束的视图拉出其父视图.您现在可以向该视图添加约束.

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读