首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在iOS 8/9中根据设备方向更改视图控制器

在iOS 8/9中根据设备方向更改视图控制器
EN

Stack Overflow用户
提问于 2015-07-17 00:44:35
回答 2查看 1.8K关注 0票数 0

在iOS 8/9中,基于设备方向在两个视图控制器之间进行排序的首选方式是什么?我想要建立一个仪器,在风景中展示。在肖像中,我想显示应用程序的设置。

EN

回答 2

Stack Overflow用户

发布于 2015-07-17 01:03:54

在"didFinishLaunchingWithOptions“函数内的AppDelegate.swift中,我放入:

代码语言:javascript
运行
复制
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)

然后在AppDelegate类中放入以下函数:

代码语言:javascript
运行
复制
func rotated(){
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation){            
        println("landscape")
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){
        println("Portrait")
    }
}

或者,您可以使用以下命令:

代码语言:javascript
运行
复制
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        println("landscape")
    } else {
        println("portraight")
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-09-19 15:03:27

这是我为iOS 9创建的机制,但这也应该适用于iOS 8。

代码语言:javascript
运行
复制
import UIKit

extension CGSize {

    static func minMaxSize(size: CGSize) -> CGSize {

        return CGSize(width: size.minValue(), height: size.maxValue())
    }

    static func maxMinSize(size: CGSize) -> CGSize {

        return CGSize(width: size.maxValue(), height: size.minValue())
    }

    func minValue() -> CGFloat {

        return min(self.width, self.height)
    }

    func maxValue() -> CGFloat {

        return max(self.width, self.height)
    }
}

extension UIScreen {

    func isPortrait() -> Bool {

        return self.bounds.width < self.bounds.height
    }
}

class BaseViewController: UIViewController {

    var horizontalConstraints = [NSLayoutConstraint]()
    var verticalConstraints = [NSLayoutConstraint]()

    private lazy var _isPortrait: Bool = UIScreen.mainScreen().isPortrait()

    func isPortrait() -> Bool {

        if let parentViewController = self.parentViewController as? BaseViewController {

            return parentViewController.isPortrait()
        }

        return _isPortrait
    }

    override func loadView() {

        self.view = UIView()
    }

    override func viewDidLoad() {

        super.viewDidLoad()

        self.addViewController()

        self.addLayoutGuides()
        self.setupLayoutGuides()

        self.addSubViews()
        self.setupSubViews()
    }

    func addViewController() {

        /* override point */
    }

    func addLayoutGuides() {

        /* override point */
    }

    func setupLayoutGuides() {

        /* override point */
    }

    func addSubViews() {

        /* override point */
    }

    func setupSubViews() {

        /* override point */
    }

    private func applyConstraints() {

        self.willApplyConstraints()

        self.isPortrait() ? self.applyVerticalConstrains() : self.applyHorizontalConstrains()

        self.didApllyedConstraints()
    }

    func willApplyConstraints() {

        /* override point */
    }

    func applyVerticalConstrains() {

        /* override point */
    }

    func applyHorizontalConstrains() {

        /* override point */
    }

    func didApllyedConstraints() {

        /* override point */


    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        if let superView = self.view.superview {

            if superView.isKindOfClass(UIWindow.self) && self.parentViewController == nil {

                if CGSize.minMaxSize(size) == CGSize.minMaxSize(UIScreen.mainScreen().bounds.size) {

                    _isPortrait = size.width < size.height
                }
            }
        }

        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        self.applyConstraints()
    }
}

至少我假设子viewController永远不会接触继承的私有_isPortrait,因为它是lazy。只有rootViewController的顶级视图才能设置此属性。超级viewWillTransitionToSize函数在检查完成后调用。因此,如果在rootViewController上设置了该值,则所有子viewController在调用其viewWillTransitionToSize函数时都将具有正确的值。这是必须要做的,因为你不能依赖于子viewController视图的大小,但至少你可以信任你的‘`rootViewController’,如果它是全屏的。希望这能以某种方式帮助到某个人。

所以我的RootViewController现在可能是这样的。

代码语言:javascript
运行
复制
class RootViewController: BaseViewController {

    override func addViewController() {

        /* add child view controller here so they can receive callbacks */
    }

    override func addSubViews() {

        /* add your subviews here in a way you like so you can play with AutoLayout as you wish */
    }

    override func setupSubViews() {

        /* setup up your views here and always activate vertical constraints here */
        /* because iOS will always initialize in portrait even if launched in landscape */
        /* deactivate your horizontal constrains and put them inside the array */
        /* add vertical constraints to the vertical array; other constraints can be active */
        /* without the need to be added somewhere */
    }

    override func willApplyConstraints() {

        /* deactivate all constrains */
        NSLayoutConstraint.deactivateConstraints(self.verticalConstraints)
        NSLayoutConstraint.deactivateConstraints(self.horizontalConstraints)
    }

    override func applyVerticalConstrains() {

        NSLayoutConstraint.activateConstraints(self.verticalConstraints)
    }

    override func applyHorizontalConstrains() {

        NSLayoutConstraint.activateConstraints(self.horizontalConstraints)
    }

    override func didApllyedConstraints() {

        /* force layouting if needed */
        self.view.layoutIfNeeded()
    }
}

更新(Xcode7-OSX10.11.1beta):用于iPad测试的使用iPad 2或iPad视网膜模拟器,因为Air 1/2与AutoLayout断开。所有模拟器都将生成以下消息:<CATransformLayer: 0x790af320> - changing property masksToBounds in transform-only layer, will have no effect

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31460049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档