0

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;}

当设备旋转到横向时,我想更改按钮框架。我试过检查interfaceOrientationshouldAutorotate...上面的方法并设置框架。我试过在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");
  }
}

并且从来没有调用过任何一个 NSLog。我的观点正在旋转...

4

2 回答 2

0

因此检查方向....

- (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);
 }
}
于 2012-04-24T21:23:54.140 回答
0

从 UIViewController 看一下:

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

看看这篇文章: willAnimateRotationToInterfaceOrientation not called on popViewControllerAnimated

于 2012-04-24T21:09:08.713 回答