![](/img/trans.png)
[英]Prototyped collection view cell “Unable to simultaneously satisfy constraints.” warning
[英]Getting “Unable to simultaneously satisfy constraints.” warning when updating height constraint at rotation
旋轉設備時,我需要更改視圖的高度。 我將storyboard
與autolayout
一起autolayout
,並且在視圖控制器中將高度約束設置為IBOutlet
:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *passwordViewHeight;
然后,在視圖控制器中,我還有方法:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
[self.view setNeedsUpdateConstraints];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
- (void)updateViewConstraints
{
[super updateViewConstraints];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationPortrait) {
[self.welcomeLabel setHidden:NO];
[self setAllPortraitConstraints];
}
else if ((orientation == UIInterfaceOrientationLandscapeLeft) ||
(orientation == UIInterfaceOrientationLandscapeRight)) {
[self.welcomeLabel setHidden:YES];
[self setAllLandscapeConstraints];
}
}
- (void)setAllLandscapeConstraints
{
[self.view removeConstraint:self.passwordViewHeight];
// More constraints
self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:34.0];
// More constraints
[self.view addConstraint:self.passwordViewHeight];
// More constraints
}
- (void)setAllPortraitConstraints
{
[self.view removeConstraint:self.passwordViewHeight];
// More constraints
self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:40.0];
// More constraints
[self.view addConstraint:self.passwordViewHeight];
// More constraints
}
然后,當我以縱向運行該應用程序並將設備旋轉為橫向時,我在Xcode
獲得了此日志:
無法同時滿足約束條件。 以下列表中至少有一個約束是您不想要的約束。 嘗試以下操作:(1)查看每個約束,並嘗試找出不期望的約束; (2)查找添加了一個或多個不必要約束的代碼並進行修復。 (注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,請參閱有關UIView屬性translationsAutoresizingMaskIntoConstraints的文檔)
(
"id: login.1, constant: 40.000000",
"id: (null), constant: 34.000000"
)
將嘗試通過打破約束id:login.1,常數:40.000000進行恢復
我想替換約束以在設備方向改變時為其賦予新的常數,我在做什么錯呢?
提前致謝
嘗試在 loadView
或一些初始化代碼中創建約束。然后將代碼更改為此:嘗試以下操作:
- (void)setAllLandscapeConstraints
{
self.passwordViewHeight.constant = 34;
}
- (void)setAllPortraitConstraints
{
self.passwordViewHeight.constant = 40;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.