我有一个依赖于iAdBanner的UIImageView位置。我收到错误:
"<NSLayoutConstraint:0x17409eb40 UIImageView:0x1741e9200.bottom == UIView:0x17418d270.bottom - 100>",
"<NSLayoutConstraint:0x174281e00 UIImageView:0x1741e9200.bottom == UIView:0x17418d270.bottom - 50>"
关于导致这种情况的任何想法?下面是设置uiimageview的代码,根据iAd是否加载,设置了它的高度。
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sunshine.png"]];
CGRect frame = imgView.frame;
frame.size.width = 180;
frame.size.height = 30;
imgView.frame = frame;
imgView.translatesAutoresizingMaskIntoConstraints = NO;
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView sizeToFit];
[self.view addSubview:imgView];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:imgView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:6.0]];
_imgView = imgView;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:_imgView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-100.0]];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:_imgView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-50.0]];
}
答案 0 :(得分:0)
据我所知,你在约束之间存在冲突,你应该删除两个约束中的一个。
答案 1 :(得分:0)
问题是您要添加2次底部约束,一次在-(void)bannerViewDidLoadAd:(ADBannerView *)banner
中,一次在(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
方法中。约束具有不同的常量值(-100和-50),因此它们会崩溃。
这样做的正确方法是只添加一次约束,然后在2种方法中添加约束值。
这样的事情:
在第一个添加尾随约束的方法中,还要添加一个底部约束并保留对它的引用:
self.bottomOffsetConstraint = [NSLayoutConstraint
constraintWithItem:_imgView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-100.0];
[self.view addConstraint:self.bottomOffsetConstraint];
然后在2种方法中更改常量:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[self.bottomOffsetConstraint setConstant:-50];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
// You need to call this in the animation block to trigger the constraints update
[self.view layoutIfNeeded];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[self.bottomOffsetConstraint setConstant:-50];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[self.view layoutIfNeeded];
[UIView commitAnimations];
}
这将修复崩溃约束并为布局更改设置动画。
让我知道它是怎么回事!