简体   繁体   English

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

[英]How to rotate view from portrait to landscape mode

I have a problem with orientations in my app. 我在我的应用程序中遇到方向问题。 Assume that I have two views (with dedicated view controllers): 假设我有两个视图(带有专用的视图控制器):

  • first should be displayed in portrait (it is displayed correctly) 首先应该以纵向显示(正确显示)
  • second should be displayed in landscape (it is not displayed correctly) 应以横向显示(显示不正确)

It is coarctated and displayed in portrait (like in second image below). 它被缩窄并以纵向显示(如下面的第二幅图像所示)。 When I rotate device horizontal and back to portrait everything is OK. 当我将设备水平旋转并回到纵向时,一切正常。 But after pushing view it displays incorrectly (images below). 但是在推送视图后,它显示不正确(下图)。 How can I fix this? 我怎样才能解决这个问题?

在此处输入图片说明

I use CustomNavigationController whish inherits from UINavigatorControler and implements three methods: 我使用CustomNavigationController继承自UINavigatorControler并实现了三种方法:

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}

In application delegate I initializing controller in this way: 在应用程序委托中,我以这种方式初始化控制器:

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];

First view controller implements orientation functions in this way: First View控制器通过以下方式实现定位功能:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

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

    return NO;
}

Second view controller implements orientation functions in this way: 第二个视图控制器以这种方式实现定位功能:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

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

    return NO;
}

hi declare a global variable BOOL isLandScape ; 嗨,声明一个全局变量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);


}

Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need. 选中问题“ 如何在iOS 6中处理不同的方向”。有关确实需要的项目示例,请参见此处的答案。

Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). 基本上,您需要在视图控制器(要旋转的视图控制器)中嵌入自定义导航控制器。 Add the following method in this custom navigation controller (this if for landscape orientation but you can change to portrait too). 在此自定义导航控制器中添加以下方法(如果用于横向,则可以更改为纵向)。

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

and add to your view controller that should rotate: 并添加到应该旋转的视图控制器中:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. 确保在项目中启用了“纵向”,“向右横向”和“向左横向”方向。 Then, if you want to block some orientations for a particular window: 然后,如果要阻止特定窗口的某些方向:

– application:supportedInterfaceOrientationsForWindow:

To do this You can use this function: 为此,您可以使用以下功能:

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];

You can use this wherever you want, but in application delegate (in didFinishLaunchingWithOptions ) i must put this code: 您可以在任何地方使用它,但是在应用程序委托中(在didFinishLaunchingWithOptions中 ),我必须放置以下代码:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

It's works perfectly! 完美的作品!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS 6及更高版本中将单个视图从纵向旋转为横向? - How to rotate a single view from portrait to landscape in iOS 6 and above? 如何将视图从纵向模式更改为横向模式并锁定它? - how to change a view from portrait mode to landscape mode and lock it? 如何自动旋转仅第一视图和第二视图仅横向模式iOS6 - How to auto rotate first view only portrait and second view only landscape mode ios6 如何从纵向视图打开横向视图 - how to open a landscape view from a portrait view 即使在 iPad 中屏幕旋转为横向,如何锁定纵向视图 - How to lock the portrait view even if screen rotate to landscape in ipad 从纵向旋转到横向时如何更改UIBarButtonItem的大小? - how to change the UIBarButtonItem size when rotate from the portrait to landscape? 如何支持纵向和横向模式 - 使用XIB的可重用视图 - How to support both Portrait and Landscape mode - reusable view with XIBs CMSampleBuffer 在 Swift 3 中从纵向旋转到横向 - CMSampleBuffer rotate from portrait to landscape in Swift 3 将视图从横向旋转到纵向 - Rotation a view from Landscape to Portrait 在仅纵向模式下伪造横向视图 - Fake a landscape view in portrait only mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM