Ios 如何将视图从纵向模式旋转到横向模式

Ios 如何将视图从纵向模式旋转到横向模式,ios,iphone,objective-c,view,orientation,Ios,Iphone,Objective C,View,Orientation,我的应用程序中的方向有问题。假设我有两个视图(使用专用视图控制器): 首先应以纵向显示(正确显示) 秒应以横向显示(未正确显示) 它被压缩并以纵向显示(如下面的第二幅图所示)。 当我水平旋转设备并返回到纵向时,一切正常。但在按下view后,它的显示不正确(下图)。我怎样才能解决这个问题 我使用CustomNavigationController,它继承了UINavigatorController并实现了三种方法: - (NSUInteger)supportedInterfaceOrient

我的应用程序中的方向有问题。假设我有两个视图(使用专用视图控制器):

  • 首先应以纵向显示(正确显示)
  • 应以横向显示(未正确显示)
它被压缩并以纵向显示(如下面的第二幅图所示)。 当我水平旋转设备并返回到纵向时,一切正常。但在按下view后,它的显示不正确(下图)。我怎样才能解决这个问题

我使用CustomNavigationController,它继承了UINavigatorController并实现了三种方法:

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}
在application delegate I中,以以下方式初始化控制器:

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationPortrait)
        return YES;

    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeRight)
        return YES;

    return NO;
}
第一视图控制器通过以下方式实现方向功能:

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationPortrait)
        return YES;

    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeRight)
        return YES;

    return NO;
}
“第二视图控制器”通过以下方式实现方向功能:

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationPortrait)
        return YES;

    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeRight)
        return YES;

    return NO;
}

hi声明一个全局变量BOOL isLandScape

initialize it as isLandScape=NO;


   - (NSUInteger)supportedInterfaceOrientations
    {
    return UIInterfaceOrientationMaskLandscapeRight;
  }


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{

  if ((orientation == UIInterfaceOrientationLandscapeRight)||(orientation == UIInterfaceOrientationLandscapeLeft))
{

      isLandScape=YES;

        return YES;

}

else

{

isLandScape=NO;

 return NO;

}

yourObject.frame=CGRectMake(isLandScape?0:0,isLandScape?0:0,isLandScape?1024:768,isLandScape?768:1024);


}

为此,您可以使用此功能:

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
您可以在任何地方使用它,但在application delegate(在didFinishLaunchingWithOptions中)中,我必须输入以下代码:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
它工作得很好

检查问题并在此处查看答案,以获取您所需的项目示例

基本上,您需要在viewcontroller(要旋转的)中嵌入自定义导航控制器。在此自定义导航控制器中添加以下方法(如果此方法用于横向,但也可以更改为纵向)

并添加到应旋转的视图控制器:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
确保在项目中启用了纵向、横向右侧和横向左侧方向。然后,如果要阻止特定窗口的某些方向,请执行以下操作:

– application:supportedInterfaceOrientationsForWindow: