我有一個視圖控制器,需要在橫向和縱向之間進行不同的佈局。除了一件小事外,下面的代碼幾乎完美地工作。在被移動的按鈕(標籤= 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];
[email protected]"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];
}
}
Upvoted-這立即解決了我的問題。我有一個沒有使用任何約束的視圖,但xib選中了「使用自動佈局」,其子視圖的位置以意想不到的方式偏移。 – bmovement