2013-07-18 27 views
1

我想在縱向模式下在屏幕底部顯示UIView,所以當手機旋轉和水平方向將重新定位/調整所有子視圖時,該UIView將保留它在哪裏,具有相同的尺寸和原始位置(即,如果它位於縱向模式的底部,則位於水平方向的右端)。UIView忽略方向或底部到底容器

有沒有很好的方法來做到這一點?

+1

我不喜歡自動佈局,但看起來像改變方向的句柄是唯一的方法。 此外這個問題必須有幫助:http://stackoverflow.com/questions/6104963/placing-and-moving-views-on-rotation-uiview?rq=1 – kaspartus

+0

我認爲做到這一點的唯一方法是不允許此控制器旋轉,然後將變換應用到要旋轉的屏幕部分。 – rdelmar

+0

你想要保留這個視圖的任何子視圖也不旋轉嗎?那麼,從這個角度來看,一切都會站在它的一邊? – rdelmar

回答

0

您可以設置自動尺寸面具是這樣的:

myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin; 

這將保持在底部的視圖。

+0

這將旋轉視圖。是的 - 它將保持在水平模式的底部,但我希望它保持在水平模式的右邊 – Vad

+0

@Vad我有點困惑於你的意思是留在右邊,請你澄清一下嗎? –

+0

簡單地說,我想要一個容器粘在Home按鈕始終位於的角落。因此,如果您處於肖像模式 - 如果您處於水平模式,則它位於屏幕的底部 - 它位於屏幕的右側或左側。 – Vad

0

我可以想到幾種方法來做到這一點。一種方式,我在下面展示的僅僅依賴於使用約束。爲了這個工作,3個按鈕不應該在他們自己的透明視圖中,而只是你想要旋轉的視圖的子視圖(在我的例子中是self.view)。我並不認爲這三個按鈕的原始約束很重要,因爲我在旋轉時移除了它們,但是我開始的約束條件是中心按鈕具有centerX約束,固定距離到底部,標準水平距離到左邊,右鍵,並且所有三個按鈕的基線對齊。在viewDidLoad中,我遍歷所有self.view的約束,並確定與這些按鈕有關的所有內容,並將它們放入數組中,以便我可以刪除它們並在稍後添加它們。

@interface ViewController() 
@property (strong,nonatomic) NSMutableArray *portraitConstraints; 
@property (strong,nonatomic) NSMutableArray *landscapeConstraints; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.portraitConstraints = [NSMutableArray array]; 
    self.landscapeConstraints = [NSMutableArray array]; 
    for (NSLayoutConstraint *con in self.view.constraints) { 
     if (con.firstItem == self.leftButton || con.secondItem == self.leftButton || con.firstItem == self.centerButton || con.secondItem == self.centerButton || con.firstItem == self.rightButton || con.secondItem == self.rightButton) { 
      [self.portraitConstraints addObject:con]; 
     } 
    } 
} 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { 

    switch (interfaceOrientation) { 
     case UIInterfaceOrientationLandscapeRight:{ 
      [self.view removeConstraints:self.portraitConstraints]; 
      [self.view removeConstraints:self.landscapeConstraints]; 
      [self.landscapeConstraints removeAllObjects]; 
      NSLayoutConstraint *centerYCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; 
      NSLayoutConstraint *rightCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeRight relatedBy:0 toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-8]; 
      NSArray *stackCons= [NSLayoutConstraint constraintsWithVisualFormat:@"V:[left]-[center]-[right]" options:NSLayoutFormatAlignAllLeading metrics:nil views:@{@"left":self.leftButton, @"center":self.centerButton, @"right":self.rightButton}]; 
      [self.landscapeConstraints addObject:centerYCon]; 
      [self.landscapeConstraints addObject:rightCon]; 
      [self.landscapeConstraints addObjectsFromArray:stackCons]; 
      [self.view addConstraints:self.landscapeConstraints]; 
      break; 
     } 
     case UIInterfaceOrientationLandscapeLeft:{ 
      [self.view removeConstraints:self.portraitConstraints]; 
      [self.view removeConstraints:self.landscapeConstraints]; 
      [self.landscapeConstraints removeAllObjects]; 
      NSLayoutConstraint *centerYCon2 = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; 
      NSLayoutConstraint *leftCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeLeft relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:8]; 
      NSArray *stackCons2= [NSLayoutConstraint constraintsWithVisualFormat:@"V:[left]-[center]-[right]" options:NSLayoutFormatAlignAllLeading metrics:nil views:@{@"left":self.leftButton, @"center":self.centerButton, @"right":self.rightButton}]; 
      [self.landscapeConstraints addObject:centerYCon2]; 
      [self.landscapeConstraints addObject:leftCon]; 
      [self.landscapeConstraints addObjectsFromArray:stackCons2]; 
      [self.view addConstraints:self.landscapeConstraints]; 
      break; 
     } 
     case UIInterfaceOrientationPortrait:{ 
      [self.view removeConstraints:self.landscapeConstraints]; 
      [self.view addConstraints:self.portraitConstraints]; 
      break; 
     } 
     default: 
      break; 
    } 
} 
+0

但是約束不適用於iOS 5,他們會嗎? – Vad

+0

@Vad,不,他們不會。你應該提到你需要在你的問題中支持iOS 5的事實(儘管我不知道你爲什麼想這麼做)。隨着iOS 7即將問世,我會認爲iOS 6和iOS 7會是所有你需要支持的)。 – rdelmar

+0

我明白了。謝謝... – Vad