一般我们可以直接重写
willRotateToInterfaceOrientation方法来实现切换横竖屏时改变控件的位置及内容,但添加到UITabBar里的view是无法触发
tab1ViewController里设置具体需要调整的控件:
willRotateToInterfaceOrientation这个方法的(只有添加到启动窗体windowView中才能触发这个事件)。
我是采用在承载tabbarcontroll的窗体上响应该方法,去改变每一个tabItem里view的控件按照屏幕方向来调整:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//切换横竖屏
[self setViewFrame:tab1ViewController FrameOrentation:toInterfaceOrientation];
[self.tab1ViewController UpdateControlByOrentation:toInterfaceOrientation];
[self setViewFrame:tab2ViewController FrameOrentation:toInterfaceOrientation];
[self setViewFrame:tab3ViewController FrameOrentation:toInterfaceOrientation];
}
tab1ViewController里设置具体需要调整的控件:
//切换横竖屏后更新mapview里的控件位置
-(void) UpdateControlByOrentation:(UIInterfaceOrientation)toInterfaceOrientation
{
CGFloat width=768;
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
width=1024;
}
self.resultView.frame=CGRectMake(self.resultView.frame.origin.x,self.resultView.frame.origin.y,self.resultView.frame.size.width, 45.0);
self.cancelBtn.frame=CGRectMake(50, self.cancelBtn.frame.origin.y, self.cancelBtn.frame.size.width, self.cancelBtn.frame.size.height);
self.resultLabel.frame=CGRectMake(width/2-self.resultLabel.frame.size.width/2,self.resultLabel.frame.origin.y,self.resultLabel.frame.size.width,self.resultLabel.frame.size.height);
self.clearBtn.frame=CGRectMake(width-50-self.clearBtn.frame.size.width, self.clearBtn.frame.origin.y, self.clearBtn.frame.size.width, self.clearBtn.frame.size.height);
}
网上查到还有一种方法是使用消息,应该也是上一层view在触发rotate事件后发送消息给各个subview,让subview再去旋转:
Observe UIDeviceOrientationDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
...
- (void)orientationDidChange:(NSNotification *)note {
NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
}