簡體   English   中英

如何將視圖從縱向模式旋轉到橫向模式

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

我在我的應用程序中遇到方向問題。 假設我有兩個視圖(帶有專用的視圖控制器):

  • 首先應該以縱向顯示(正確顯示)
  • 應以橫向顯示(顯示不正確)

它被縮窄並以縱向顯示(如下面的第二幅圖像所示)。 當我將設備水平旋轉並回到縱向時,一切正常。 但是在推送視圖后,它顯示不正確(下圖)。 我怎樣才能解決這個問題?

在此處輸入圖片說明

我使用CustomNavigationController繼承自UINavigatorControler並實現了三種方法:

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

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

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

在應用程序委托中,我以這種方式初始化控制器:

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

First View控制器通過以下方式實現定位功能:

- (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;
}

嗨,聲明一個全局變量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);


}

選中問題“ 如何在iOS 6中處理不同的方向”。有關確實需要的項目示例,請參見此處的答案。

基本上,您需要在視圖控制器(要旋轉的視圖控制器)中嵌入自定義導航控制器。 在此自定義導航控制器中添加以下方法(如果用於橫向,則可以更改為縱向)。

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

並添加到應該旋轉的視圖控制器中:

- (BOOL)shouldAutorotate
{
    return YES;
}

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

確保在項目中啟用了“縱向”,“向右橫向”和“向左橫向”方向。 然后,如果要阻止特定窗口的某些方向:

– application:supportedInterfaceOrientationsForWindow:

為此,您可以使用以下功能:

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

您可以在任何地方使用它,但是在應用程序委托中(在didFinishLaunchingWithOptions中 ),我必須放置以下代碼:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

完美的作品!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM