簡體   English   中英

如何重新排列以編程方式創建的旋轉視圖?

[英]How to rearrange programmatically created view on rotation?

ViewControllerA有一個viewDidLoad方法,該方法以編程方式創建一個按鈕,例如:

self.cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.cancelButton.frame = CGRectMake(330, 625, 125, 30);
[self.cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[self.cancelButton sizeToFit];
[self.cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchUpInside];
[self.cancelButton setNeedsDisplay];
[self.view addSubview:self.cancelButton];

我也有- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{return YES;}

將設備旋轉到橫向時,我想更改按鈕框。 我試過在我的shouldAutorotate...方法中檢查interfaceOrientation並設置框架。 我試着做if (UIInterfaceOrientationLandscapeLeft == [[UIApplication sharedApplication] statusBarOrientation])並在那里重置框架。 這些都不起作用。 有什么建議么?

編輯

這是沒有任何變化的另一種嘗試:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
  self.cancelButton.frame = CGRectMake(15, 15, 300, 50);
  } 
}

所以我試着做:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  if (toInterfaceOrientation == UIInterfaceOrientationPortrait
  || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
    NSLog(@"Portait");
  } else {
    NSLog(@"Landscape");
  }
}

而且,從未調用過任何NSLogs。 我的觀點雖然在旋轉...

從UIViewController看一下:

– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:

看一下這篇文章: popViewControllerAnimated上未調用willAnimateRotationToInterfaceOrientation

因此檢查方向。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
 {
  // portrait & upsidedown
  self.cancelButton.frame = CGRectMake(330, 625, 125, 30);
 }
 else
 {
    // landscape
    self.cancelButton.frame = CGRectMake(330, 625, 125, 30);
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM