iOS的橫屏(Landscape)與豎屏(Portrait)InterfaceOrientation

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   使用   for   sp   strong   

轉自:http://www.molotang.com/articles/1530.html

接著上篇寫的觸摸事件,這次藉機會整理下iOS橫屏和豎屏的翻轉方向支援,即InterfaceOrientation相關的內容

最近做一個頁面,最初並沒有太多考慮orientation的情況,當其嵌入到一個在iPad上使用橫屏(Landscape)的應用中,就會只顯示在螢幕的左面,而且貌似還沒顯示全,這個……很醜!發自內心地覺得這麼做對不起蘋果的設計理念!對不起喬老爺子。。。

改!說到該就要瞭解蘋果開發中對iOS應用的橫屏(Landscape)和豎屏(Portrait)的支援情況。

0. 應用層級的配置

大家(特指有iOS開發經驗的人)應該都知道Xcode Project的工程配置General頁簽中有那麼四個圖(或者4個checkbox),標識對四種interfaceOrientation的支援。分別為Portrait、PortraitUpsideDown、LandscapeLeft和LandscapeRight。

對應的,在Xcode Project工程配置的Info頁,實際上就是Info.plist中,有對4種Orientation的記錄項。

這兩者是一樣的。

1. Window層級的控制

在iOS6.0之後,UIApplicationDelegate中多了一個方法聲明:

1 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

就是對於特定的application和特定的window,我們需要支援哪些interfaceOrientation,這是可以通過實現這個方法定製的。

傳回值是一個不帶正負號的整數,實際上是可以使用定義好的枚舉值:

123456789 typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),};

對於UIApplicationDelegate的這個方法聲明,大多數情況下application就是當前的application,而window通常也只有一個。所以基本上通過window對橫屏豎屏interfaceOrientation的控制相當於全域的。

2. Controller層面的控制

老版本的iOS有這樣一個方法:

1 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);

即定製是否可以旋轉到特定的interfaceOrientation。

而在iOS6之後,推出了2個新的方法來完成這個任務:

12 - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);

可以看得出來,兩個和在一起就是原來任務的完成過程。其中,大概的判斷方式是,先執行前者,判斷是否可以旋轉,如果為YES,則根據是否支援特定的interfaceOrientation再做決斷。

3. 使得特定ViewController堅持特定的interfaceOrientation

iOS6之後還提供了這樣一個方法,可以讓你的Controller倔強第堅持某個特定的interfaceOrientation:

1 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);

這就叫堅持,呵呵!
當然,這裡使用的是另外一套枚舉量,可以去UIApplication.h中查看定義。

4. 當前螢幕方向interfaceOrientation的擷取

有3種方式可以擷取到“當前interfaceOrientation”:

  • controller.interfaceOrientation,擷取特定controller的方向
  • [[UIApplication sharedApplication] statusBarOrientation] 擷取狀態條相關的方向
  • [[UIDevice currentDevice] orientation] 擷取當前裝置的方向

具體區別,可參見StackOverflow的問答:

http://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation

5. 容器Controller的支援

上面把interfaceOrientation方向的擷取和支援配置都說了,看起來沒什麼問題了。有沒有什麼特殊情況?

當你使用TabbarController和NavigationController按照如上做法使用的時候就會有些頭疼。

辦法不是沒有,比較通俗的一種就是——繼承實現。

(補充:iOS7之後有delegate可以對此進行控制)

關於iOS interface orientation螢幕方向的內容就整理到此,歡迎各位看官發言。

 0 相關文章:
  • 2013 年 12 月 20 日 -- iOS應用程式的狀態及其切換(生命週期)
  • 2013 年 12 月 18 日 -- iOS應用喚起和定製URL Scheme
  • 2014 年 2 月 22 日 -- iOS的後台運行和多任務處理
  • 2014 年 5 月 11 日 -- iOS中block的使用
此條目發表在 iOS, iOS開發基礎, 電腦技術 分類目錄,貼了 interfaceOrientation, iOS, Landscape, Portrait,UIApplicationDelegate, UIViewController, UIWindow, 螢幕方向 標籤。將固定連結加入收藏夾。

iOS的橫屏(Landscape)與豎屏(Portrait)InterfaceOrientation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.