我正在使用自动布局技术来设计视图。我已经使用滚动条进行了多次自动布局。但今天我得到了一个奇怪的例外。
异常
-(void)allocScrollVieww:(UIView*)Mview
{
scrll=[UIScrollView new];
[scrll setTranslatesAutoresizingMaskIntoConstraints:NO];
[Mview addSubview:scrll];
SBV=[UIView new];
[SBV setTranslatesAutoresizingMaskIntoConstraints:YES];// as i can't add constraints on `scrollView` so I have added a view on `scrollView`
[scrll addSubview:SBV];
}
-(void)scrllView:(UIView*)Mview
{
[Mview addConstraint:[NSLayoutConstraint constraintWithItem:scrll attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:Mview attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
[Mview addConstraint:[NSLayoutConstraint constraintWithItem:scrll attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:Mview attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
[Mview addConstraint:[NSLayoutConstraint constraintWithItem:scrll attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:Mview attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
[Mview addConstraint:[NSLayoutConstraint constraintWithItem:scrll attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:Mview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
[scrll setContentSize:CGSizeMake(Mview.frame.size.width, Mview.frame.size.height+100)];
[SBV setFrame:CGRectMake(scrll.frame.origin.x, scrll.frame.origin.y, scrll.frame.size.width, scrll.frame.size.height+100)];
-(void)BuyotherView:(UIView*)views
{
//green label
UILabel *greebLabel1=[UILabel new];
[greebLabel1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[greebLabel1 setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:0.2 alpha:0.3]];
[views addSubview:greebLabel1];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
- (void)viewDidLoad {
[self allocScrollVieww:self.view];
[self scrllView:self.view];
[self BuyotherView:SBV];
[super viewDidLoad];
}
我没有添加任何乘数为零,那么为什么它会显示此异常
代码:
{{1}}
答案 0 :(得分:1)
没有答案!没问题,我自己找到了解决方案。
<强>代码强>
-(void)BuyotherView:(UIView*)views
{
//green label
UILabel *greebLabel1=[UILabel new];
[greebLabel1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[greebLabel1 setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:0.2 alpha:0.3]];
[views addSubview:greebLabel1];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
//navBar is associated to other view. And it is illegal to use constarint of one UIVIew object of one view to another.
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:navBar attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
我将最后一个约束更改为:
[views addConstraint:[NSLayoutConstraint constraintWithItem:greebLabel1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:44*0.7]];
它开始工作正常:)