gpt4 book ai didi

ios - 在 ScrollView 上的 View 上添加约束显示异常

转载 作者:行者123 更新时间:2023-11-29 12:13:25 27 4
gpt4 key购买 nike

我正在使用自动布局技术来设计 View 。我已经用滚动条完成了很多次自动布局。但是今天我遇到了一个奇怪的异常。

异常

[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs'

我没有添加任何零乘数,那么为什么会显示此异常

代码:

-(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];

}

最佳答案

没有答案!没问题,我自己找到了解决方案。

代码

-(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]];

它开始正常工作:)

关于ios - 在 ScrollView 上的 View 上添加约束显示异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com