昨天朋友問我,怎麼實如今豎屏的應用裏,顯示一個橫屏的應用,因爲也沒作過測試
就說不知道了,可是我以爲會有這樣的API ,由於我手機裏就安裝有這種類型的軟件spa
今天早上起來,就想作一個Demo出來,慣例的是查找資料,測試是否可使用,可是code
查找了都寫的不是很清楚,很容易形成無法實現想要的效果,因此想從新寫過一個blog
但願能幫到有這個需求的朋友。(這個沒什麼經驗,的是照着網上的資料拼湊起來,it
若是有錯誤,請見諒,有更好的辦法也能夠告訴我)io
實現的效果:ast
頁面大部分是豎屏,個別頁面能夠旋轉手機時頁面變爲橫屏(其實朋友的需求是,進入class
這個頁面就是橫屏的,不知道怎麼弄)。軟件
實現步驟:方法
1.使應用能夠橫屏,新建立的默認就能夠
2.在跟視圖裏實現如下的方法(導航控制器和TabBar控制器的能夠)
-(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return [self.viewControllers.lastObject supportedInterfaceOrientations]; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation]; }
3.在不須要橫屏的視圖控制器裏面實現如下方法
-(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait ; } - (BOOL)shouldAutorotate { return NO; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }
4,須要橫屏的頁面不須要其餘的處理就能夠了。