重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
主要下面三個方法
創新互聯建站是一家專注于網站建設、網站設計與策劃設計,金水網站建設哪家好?創新互聯建站做網站,專注于網站建設十年,網設計領域的專業建站公司;建站業務涵蓋:金水等地區。金水做網站價格咨詢:028-86922220
1、當前viewController是window的rootViewController.
2、當前viewController是modal模式的. 即, 此viewController是被調用
presentModalViewController 而顯示出來的.
對于非modal模式的viewController:如果不是rootViewController,則重寫 supportedInterfaceOrientations , preferredInterfaceOrientationForPresentation 以及 shouldAutorotate 方法, 按照當前viewController的需要返回響應的值.如果是rootViewController,則如下重寫方法:
這樣就繞開了UIKit只調用rootViewController的方法的規則. 把決定權交給了當前正在顯示的viewController.
對于modal
模式的viewController. 則按照需要重寫supportedInterfaceOrientations,
preferredInterfaceOrientationForPresentation
以及shouldAutorotate 方法即可.
TIPS:
[UIViewController attemptRotationToDeviceOrientation]; 可以重新調用以上的方法。
雖然A界面不支持橫屏,但是如果下個B界面支持橫屏,B在橫屏模式返回的話,A也變橫屏了。這里有個簡單的設值方法
A界面也重寫方法即可
重寫
有時候想自己控制橫豎屏 ,比如視圖播放界面。
setOrientation 在iOS3以后變為私有方法了,不能直接去調用此方法,否則后果就是被打回。
不能直接調用,但是可以間接的去調用,下面的方法就是利用 KVO機制去間接調用.
前段時間我們播放器強制橫屏,項目設置允許豎屏,在手機不鎖屏狀態下,手機橫屏會導致播放器強制橫屏的時候會導致橫屏失敗,下面是強制橫屏的解決辦法以及我的探究.
首先,在【General】--【Device Orientation】設置僅支持豎屏
接下來在AppDelegate中設置
先設置
接下來有兩種辦法可以在某個界面設置強制橫屏
第一種.先把設備狀態設置為豎屏,再強制橫屏
第二種.設置強制橫屏,再調用
這樣就解決手機不鎖屏狀態下的強制橫屏導致的問題,但是為什么產生這些問題呢,請看下我接下來的探究.
接下來說下橫屏失敗的原因:
用戶先向左轉了設備的方向才點擊橫屏,在強制頁面向右(也就是設備向左)橫屏時,雖然項目是只允許豎屏的,屏幕界面方向也一直是豎屏的,但設備本身的方向(也就是[UIDevice currentDevice].orientation)其實已經是向左,此時其實kvc強制設置的值和本來的值是一樣的,這就導致了屏幕界面不轉動,所以可以先強制轉到另一個方向再轉回來,或者使用attemptRotationToDeviceOrientation方法使屏幕界面和設備方向同步。其他方向同理,另外,若用戶啟用了設備方向鎖,用戶無論如何旋轉設備其設備方向都不變,也就不會有此問題。
下面是我的思路:
在屏幕沒有鎖定的時候,手機為橫屏的時候,播放器橫屏時候沒有橫屏
但是播放器界面并沒有變為全屏,根據效果我覺得有可能是寬高問題導致的,因此我先獲取手機的屏幕方向
在獲取手機的設備方向
發現屏幕方向和設備方向不一致
接下來使屏幕方向和設備方向一致,就可以解決問題
參考文章
iOS 獲取屏幕方向
當自定義一個UIWindow,并在window添加控件,橫屏時,window并沒有跟隨視圖旋轉。
解決方法1:(蘋果推薦這樣使用)
1.定義一個UIViewController,并設置為當前Window的rootViewController,將控件添加到自定義的UIViewController上,調用時使用self.mineWindow.mineRootViewController.button...
2.在自定義的UIViewController中添加橫屏方法:
- (BOOL)shouldAutorotate {
returnYES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
解決方法2:
對UIWindow進行旋轉(UIWindow繼承自UIView):
UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
CGAffineTransform rotation = CGAffineTransformMakeRotation(3*M_PI/2);
[self setTransform:rotation];
}
if (orientation == UIInterfaceOrientationLandscapeRight) {
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI/2);
[self setTransform:rotation];
}
如有不當之處請@我。
在你想支持橫豎屏的viewController里面重寫兩個方法:
1
2
3
4
5
6
7
8
9
10
11
// 支持設備自動旋轉
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持橫豎屏顯示
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
這樣在這個viewController中就可以橫豎屏切換了。
注意如果你window的rootViewController是一個navigationController,可能會出現以下問題:
你的navigationController只支持豎屏,但是你push到了某個新的controller中,這個controller支持橫豎屏,當你在新的controller中切換到橫屏后(也有可能在切換到橫屏然后pop回來后),這時候程序會閃退,因為你的navigationController不支持橫屏。
如果你想解決這個問題,就需要自己寫一個UINavigationController的子類,在這個類中重寫方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (BOOL)shouldAutorotate
{
return [self.viewControllers.lastObject shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
然后用這個類去創建實例作為window的rootViewController,這樣就可以避免這個問題了。
1、iPhone6 Plus共有兩種顯示模式,分別為標準和放大模式官網 留言只有在“標準模式”下,才支持橫屏。所以要確保顯示模式在標準模式狀態下。
2、在控制中心將“方向鎖定”關閉即可讓iPhone Plus在手機橫放時自動變成橫屏顯示。
iPhone6 Plus顯示模式更改方法
激活蘋果6手機時,會讓你選擇顯示模式,如果要更改就看下面的步驟。
1、點擊主屏上的“設置”
2、在設置選項列表中找到“顯示與亮度”。
3、點擊“顯示模式”
4、選擇“放大”或者“標準”模式
5、切換不同的顯示模式之后需要重啟蘋果手機才能生效