2011-05-24 51 views
7

是否有人知道是否有可能爲一個視圖以編程方式鎖定iPhone自動旋轉? 我想用半透明的視角做出某種幫助,但是我只想支持橫向方向,即使所有其他視圖都可以旋轉。 所以我希望在這個視圖位於頂部時鎖定旋轉角度。以編程方式自動旋轉鎖定

TNX

編輯:更多細節:一個UIController有7 UIView的......我希望在頂部的最後一個出現,只是鎖定自轉。

回答

4

使用以下...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 
+4

在iOS 6.0中不贊成使用。改爲支持supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation方法) – MOBYTELAB 2013-09-21 19:48:21

0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {      
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 
3

你可以把它連到窗口。加載視圖後,請執行以下操作:

[self.view.window addSubview:yourStaticView]; 
[self.view.window bringSubviewToFront:yourStaticView]; // Do only if necessary 

離開此視圖時將其刪除。可能在viewWillDisappear:viewDidDisappear:

0

我發現了非常簡單和工作的解決方案。通過添加頂視圖,我設置了一個BOOL,然後控制旋轉。

 

// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations; we support all. 

    if (helpViewOnTheTop) { 
     return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
    } 
    else 
    { 
     return YES; 
    } 
} 


2
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    //If you don't want to support multiple orientations uncomment the line below 
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait); 
    //return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 
14

這個問題被問了一年前,但現在接受的方法是deprecated in iOS 6所以如果有人有興趣做這在iOS 6中,那麼你需要在最上面的控制器上使用supportedInterfaceOrientations。

想象一下,你有這樣的設置...

  • 標籤欄控制器
    • 導航控制器
      • 視圖控制器

...然後您需要在選項卡欄控制器上設置supportedInterfaceOrientations方法。

創建標籤欄控制器(或如果是在頂部導航控制器)的一個子類,並設置它這些方法...

- (BOOL)shouldAutorotate { 
    //Use this if your root controller is a navigation controller 
    return self.visibleViewController.shouldAutorotate; 
    //Use this if your root controller is a tab bar controller 
    return self.selectedViewController.shouldAutorotate; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    //Navigation Controller 
    return self.visibleViewController.supportedInterfaceOrientations; 
    //Tab Bar Controller 
    return self.selectedViewController.supportedInterfaceOrientations; 
} 

...然後在您的個性化視圖控制器,你可以設置你想要的屬性...

- (BOOL)shouldAutorotate { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations{ 
    //return supported orientation masks 
    return UIInterfaceOrientationMaskLandscape; 
} 
+0

這裏是最好的答案:)我已經用所提出的方法噴濺了所有視圖控制器,直到我已經添加到頂級控制器相同的方法但引用(選擇或可見)視圖控制器。 – 2014-05-06 07:54:59

1

我覺得有一些工作間歇許多具體的答案,但沒有對應用程序或其他的剩餘部分提供洞察力,以什麼後果或副作用,就視圖控制器,如果用戶開始向外傾斜手機你可能會意識到(像我一樣)可能會出現不良或不希望的結果(例如,當你不希望它們發生方向變化時,反之亦然)。

我的主要實現涉及只有'根視圖控制器'將調用'shouldAutorotate',而不只是任何你試圖覆蓋的單獨的視圖控制器。

通過這種認識,爲特定視圖控制器「鎖定」特定方向似乎相當困難。

(意已vc_A總是肖像,不允許改爲橫向,同時具有vc_B總是景觀,不允許更改爲縱向)承認這一點後

,下面的算法是什麼爲我工作只能在指定的視圖控制器上旋轉。

設置:

首先你必須讓你的願望的方向,無論是在info.plist中或主項目設置文件(這些方向將是唯一的,你可以在你的代碼中使用)

enter image description here

代碼:在我的根視圖控制器(這裏

1):MasterViewC ontroller)我指定了一個BOOL屬性(allowAutorotate),當'shouldAutorotate'被調用時將被使用。

2)也使得根視圖控制器成爲一個單元,因此可以從任何其他子視圖控制器輕鬆訪問(無需傳遞引用)。

注意:您還可以使用觀察者/通知模式或委託或其他一些模式,但對我來說,單例模式是最簡單的

3)添加代理「 - (BOOL)shouldAutorotate」,並利用BOOL allowAutorotate返回

4)創建實例方法'setInterfaceOrientation'。一些其他類將調用此方法在他們的「viewDidLoad中」和/或在他們的「viewWillDisappear」

// 1) 
@implementation MasterViewController { 
    BOOL allowAutorotate; 
} 

// 2) 
+ (id)sharedMasterViewController { 
    static MasterViewController *sharedMasterViewController = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
    sharedMasterViewController = [[self alloc] init]; 
    }); 
    return sharedMasterViewController; 
} 
- (id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     allowAutorotate = NO; 
    } 

    return self; 
} 

// 3) 
- (BOOL)shouldAutorotate 
{ 
    return allowAutorotate; 
} 

// 4) 
- (void)setInterfaceOrientation:(NSInteger)orientation 
{ 
    allowAutorotate = YES; 

    NSNumber *value = [NSNumber numberWithInt:orientation]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

    allowAutorotate = NO; 
} 

5)最後在其它的類獲得根視圖控制器和調用「setInterfaceOrientation」相應

// 5) 
#import "MasterViewController.h" 

@implementation SomeViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    [[MasterViewController sharedMasterViewController] setInterfaceOrientation:UIInterfaceOrientationLandscapeRight]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [[MasterViewController sharedMasterViewController] setInterfaceOrientation:UIDeviceOrientationPortrait]; 
} 

筆記:

1)這個例子的結果應該是應用程序最初將加載肖像,那麼當您在祛瘀加載「SomeViewController」,它將改變爲橫向,然後它會變回肖像。

2)它的工作原理是這樣的...

每次物理傾斜手機時間

,委託「shouldAutorotate」被調用(只從「根視圖控制器」),

以及每次編程傾斜手機

NSNumber *value = [NSNumber numberWithInt:orientation]; 
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

代理'shouldAutorotate'被調用。

這就是爲什麼我們第一 'allowAutorotate = YES;',然後 '傾斜手機',然後 'allowAutorotate = NO;'

因此,我們只允許/執行方向更改一次,以編程方式,完全取決於我們的期望。

glhf!