横向きと縦向きでレイアウトを変える必要があるView Controllerがあります。次のコードは、1 つの小さな点を除いてほぼ完全に機能します。移動したボタン (tag=3 のボタン) の場合、もう一方のボタン (tag=0 で (IBAction)myButton: を呼び出す) をクリックすると、ボタンは配置された位置に移動します。ストーリーボードで。ラベルは動かず、ボタンだけです。これは、(IBAction)myButton に設定されているタイトルの値が、ボタンが押される前の値と異なる場合にのみ発生します。
- (IBAction)myButton:(UIButton *)sender {
UIButton *but2 = (UIButton * )[self.view viewWithTag:3];
[but2 setTitle: @"A" forState:UIControlStateNormal];
UILabel *lab =(UILabel *)[self.view viewWithTag:2];
lab.text=@"B";
}
-(void)rotateViewToPortrait{
UIButton *but2 = (UIButton * )[self.view viewWithTag:3];
[but2 setFrame:CGRectMake(100, 500, but2.frame.size.width, but2.frame.size.height)];
UILabel *lab =(UILabel *)[self.view viewWithTag:2];
[lab setFrame:CGRectMake(100,550 , lab.frame.size.width, lab.frame.size.height)];
}
-(void)rotateViewToLandscape{
UIButton *but2 = (UIButton * )[self.view viewWithTag:3];
[but2 setFrame:CGRectMake(500, 100 , but2.frame.size.width, but2.frame.size.height)];
UILabel *lab =(UILabel *)[self.view viewWithTag:2];
[lab setFrame:CGRectMake(500,150 , lab.frame.size.width, lab.frame.size.height)];
}
-(void)viewDidAppear:(BOOL)animated{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if ((orientation==UIInterfaceOrientationLandscapeRight)||(orientation==UIInterfaceOrientationLandscapeLeft)) {
[self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationLandscapeRight duration:0];
} else {
[self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationPortrait duration:0];
}
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)||(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)) {
[self rotateViewToLandscape];
} else if ((toInterfaceOrientation == UIInterfaceOrientationPortrait)||(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){
[self rotateViewToPortrait];
}
}