IOS添加约束的时候不能使用属性和全局变量吗
发布网友
发布时间:2022-05-29 19:05
我来回答
共2个回答
热心网友
时间:2023-11-08 02:24
可以.
我直接把部分代码copy了
@property (weak, nonatomic) NSLayoutConstraint *xConstraint;
@property (weak, nonatomic) NSLayoutConstraint *yConstraint;
@property (weak, nonatomic) NSLayoutConstraint *widthConstraint;
@property (weak, nonatomic) NSLayoutConstraint *heightConstraint;
在show界面的时候
[rootViewController.view addSubview:aViewController.backgroundView]; //值得注意的代码实际上就这一句, 需要先addSubView, 再添加约束
[rootViewController.view addConstraint:[NSLayoutConstraint constraintWithItem:aViewController.backgroundView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:rootViewController.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
[rootViewController.view addConstraint:[NSLayoutConstraint constraintWithItem:aViewController.backgroundView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:rootViewController.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
[rootViewController.view addConstraint:[NSLayoutConstraint constraintWithItem:aViewController.backgroundView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:rootViewController.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[rootViewController.view addConstraint:[NSLayoutConstraint constraintWithItem:aViewController.backgroundView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:rootViewController.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
热心网友
时间:2023-11-08 02:25
是的!!不能使用了