0

ViewControllerAviewDidLoad次のようなボタンをプログラムで作成するメソッドがあります。

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;}ます。

デバイスを横向きに回転させたときに、ボタン枠を変更したい。上記の方法をチェックinterfaceOrientationしてshouldAutorotate...、フレームを設定しようとしました。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 も呼び出されません。視界がぐるぐるしてるけど…

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:

この投稿を見てください: popViewControllerAnimated で willAnimateRotationToInterfaceOrientation が呼び出されない

于 2012-04-24T21:09:08.713 に答える