3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以將這些方法粘貼到每個(gè)需要縱向顯示的視圖的ViewController中:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
Hi for LandscapeLeft和LandscapeRight (更新Swift 2.0)
你有這個(gè)信息
和UIController
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight]
}
對(duì)于PortraitUpsideDown和Portrait使用
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false
}
else {
return true
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}
法國(guó)寄語(yǔ),圣誕快樂(lè)!
編輯:
其他解決方案:
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
if visibleViewController is MyViewController {
return true // rotation
} else {
return false // no rotation
}
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
}

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果將視圖控制器嵌入U(xiǎn)INavigationController或UITabBarController中,則定向旋轉(zhuǎn)會(huì)更加復(fù)雜,導(dǎo)航或選項(xiàng)卡欄控制器將具有優(yōu)先權(quán)并就自動(dòng)旋轉(zhuǎn)和支持的定向做出決策。
在UINavigationController和UITabBarController上使用以下擴(kuò)展,以便嵌入在這些控制器之一中的視圖控制器可以做出決定:
UINavigationController擴(kuò)展
extension UINavigationController {
override open var shouldAutorotate: Bool {
? ? get {
? ? ? ? if let visibleVC = visibleViewController {
? ? ? ? ? ? return visibleVC.shouldAutorotate
? ? ? ? }
? ? ? ? return super.shouldAutorotate
? ? }
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
? ? get {
? ? ? ? if let visibleVC = visibleViewController {
? ? ? ? ? ? return visibleVC.preferredInterfaceOrientationForPresentation
? ? ? ? }
? ? ? ? return super.preferredInterfaceOrientationForPresentation
? ? }
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
? ? get {
? ? ? ? if let visibleVC = visibleViewController {
? ? ? ? ? ? return visibleVC.supportedInterfaceOrientations
? ? ? ? }
? ? ? ? return super.supportedInterfaceOrientations
? ? }
?}}
UITabBarController擴(kuò)展
extension UITabBarController {
override open var shouldAutorotate: Bool {
? ? get {
? ? ? ? if let selectedVC = selectedViewController{
? ? ? ? ? ? return selectedVC.shouldAutorotate
? ? ? ? }
? ? ? ? return super.shouldAutorotate
? ? }
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
? ? get {
? ? ? ? if let selectedVC = selectedViewController{
? ? ? ? ? ? return selectedVC.preferredInterfaceOrientationForPresentation
? ? ? ? }
? ? ? ? return super.preferredInterfaceOrientationForPresentation
? ? }
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
? ? get {
? ? ? ? if let selectedVC = selectedViewController{
? ? ? ? ? ? return selectedVC.supportedInterfaceOrientations
? ? ? ? }
? ? ? ? return super.supportedInterfaceOrientations
? ? }
}}
現(xiàn)在,您可以在要鎖定的視圖控制器中重寫(xiě)supportedInterfaceOrientations,shouldAutoRotate和preferredInterfaceOrientationForPresentation,否則可以在其他視圖控制器中忽略要繼承應(yīng)用程序plist中指定的默認(rèn)方向行為的替代。
鎖定特定方向
class YourViewController: UIViewController {
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
? ? get {
? ? ? ? return .portrait
? ? }
}}
禁用旋轉(zhuǎn)
? ? class YourViewController: UIViewController {
open override var shouldAutorotate: Bool {
? ? get {
? ? ? ? return false
? ? }
}}
更改演示的首選界面方向
class YourViewController: UIViewController {
open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
? ? get {
? ? ? ? return .portrait
? ? }
}}
- 3 回答
- 0 關(guān)注
- 1063 瀏覽
添加回答
舉報(bào)