我有一个视图控制器,需要在横向和纵向之间进行不同的布局。除了一件小事之外,以下代码几乎可以完美运行。对于移动的按钮(带有标签=3 的按钮),当我单击另一个按钮(标签=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];
}
}