如何在iPhone中更改方向时更改viewcontroller的视图?

时间:2011-07-05 08:49:06

标签: iphone orientation

我正在开发支持2种方向的iPhone应用程序。我有这个视图控制器的2个UIView文件。我需要根据设备接口方向将相应的UIView文件设置到视图控制器。你能指导我如何改变方向吗?

3 个答案:

答案 0 :(得分:3)

假设您已设置-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation以返回支持的方向,您可以使用以下方法,这些方法都可以在UIViewController中设置,以检查方向更改,然后设置视图:

// called just before the user interface starts to rotate - my advice would be to use this method to save a copy of the current orientation.
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

// called after the interface has finished rotating. Here, you can compare the old orientation with the new one - if you went from portrait to landscape, then update your views accordingly.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

希望这有帮助!

答案 1 :(得分:3)

为什么要为两个方向使用两个视图?您可以设置控件的autoresizemask属性,如果纵向和横向两种模式具有相似的控件,则可以在同一位置显示。否则,您需要在

中使用两个视图
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

或者你可以收到UIDevice的通知

 UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;

- (void)beginGeneratingDeviceOrientationNotifications;      // nestable
- (void)endGeneratingDeviceOrientationNotifications;

答案 2 :(得分:2)

只需在控制器中为这两个方向提供此委托。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
相关问题