簡體   English   中英

如何在IOS7中的多個視圖上處理設備方向?

[英]How to handle device orientation on multiple view in IOS7?

我正在開發一個應用程序,其中有兩個ViewControllers名稱分別為VC1和VC2,VC1應該同時支持縱向和橫向方向,這意味着縱向應顯示地圖,橫向應顯示列表。 現在,對於VC2,它僅適用於縱向模式。 我正在為兩個ViewController附加圖像 在此處輸入圖片說明

在此處輸入圖片說明

在第二張圖片中,谷歌地圖應該是縱向的並且是橫向的,但是當我從縱向更改為橫向時,列表僅以縱向模式生成。

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

如果您的rootViewController是UINavigationController那么您需要使用UINavigationController類別。

UINavigationController的+ autoRotate.h

#import <UIKit/UIKit.h>

@interface UINavigationController (autoRotate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

@end

UINavigationController的+ autoRotate.m

#import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

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

-(BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}

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

@end

然后在您的viewController中使用以下方法

#pragma mark -
#pragma mark - UIInterfaceOrientation Methods

// for iOS 6 and latter
-(BOOL) shouldAutorotate
{
    // return as you want;
    return YES;
}

// for iOS 5 and lower
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // return as you want;
    return  YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    //UIInterfaceOrientationMask as you want 
    return UIInterfaceOrientationMaskPortrait;
}

如果您在應用中使用UINavigationController ,則-(BOOL)shouldAutorotate應該不會在ViewController中被調用。 在這種情況下,您必須將UINavigationController子類化。

@interface RotationAwareNavigationController : UINavigationController

@end

@implementation RotationAwareNavigationController

-(NSUInteger)supportedInterfaceOrientations {
    UIViewController *top = self.topViewController;
    return top.supportedInterfaceOrientations;
}

-(BOOL)shouldAutorotate {
    UIViewController *top = self.topViewController;
    return [top shouldAutorotate];
}

@end

暫無
暫無

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

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