2015-11-08 63 views
0

我需要在旋轉設備時更改視圖的高度。我使用storyboardautolayout,我有高度限制在視圖控制器設置爲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

無法同時sa脆弱的約束。 以下列表中的至少一個約束可能是您不想要的。試試這個:(1)看看每個約束,並試圖找出你不期望的; (2)找到添加不需要的約束或約束並修復它的代碼。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,請參閱文檔UIView的財產translatesAutoresizingMaskIntoConstraints)

(
    "id: login.1, constant: 40.000000", 
    "id: (null), constant: 34.000000" 
) 

將嘗試打破約束 ID進行恢復:login.1 ,常數:40.000000

我想用設備方向改變時給它​​一個新的常量,我做錯了什麼?

在此先感謝

回答

0

嘗試loadView或一些初始化代碼創建約束。那麼你的代碼改成這樣: 試試這個:

- (void)setAllLandscapeConstraints 
{ 
    self.passwordViewHeight.constant = 34; 
} 

- (void)setAllPortraitConstraints 
{ 
    self.passwordViewHeight.constant = 40; 
} 
+0

約束在故事板創建,使所有需要的是你所示的代碼。 – Paulw11

+0

謝謝。 'updateViewConstraints'是進行更改的正確位置嗎?我在其他地方發現了一些其他的帖子,例如'viewWillTransitionToSize:'...我不明白與自動佈局有關的佈局,約束和旋轉的方法... – AppsDev