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

在iPhone中更改方向时,如何更改viewcontroller的视图?,iphone,orientation,Iphone,Orientation,我正在开发一个支持两种方向的iPhone应用程序。我有2个用于此视图控制器的UIView文件。我需要根据设备接口方向为视图控制器设置相应的UIView文件。你能告诉我如何改变方向吗?只要在控制器中为这名学员提供两个方向 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } 假设您已设置-(BOOL)shouldAutorota

我正在开发一个支持两种方向的iPhone应用程序。我有2个用于此视图控制器的UIView文件。我需要根据设备接口方向为视图控制器设置相应的UIView文件。你能告诉我如何改变方向吗?

只要在控制器中为这名学员提供两个方向

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

假设您已设置
-(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

希望这有帮助

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
或者,您可以获得UIDevice的通知

 UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;

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