自适应布局为iPad [英] Adaptive layout for iPad

查看:326
本文介绍了自适应布局为iPad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这篇文章中的ios 建设适应建设适应布局用户界面适用于iOS 8

这将很好地工作在iPhone上。
现在我wan't给iPad上同样的效果。但iPad的适应性布局是不行​​的。结果
这里是iPhone5的(绿框重新present厂景和黄色的盒子重新present视图2)应用截图结果
1.Portrait模式

<醇开始=2>
  • 风景模式结果

  • :如何实现为iPad相同的作用结果
    扩展问题:如何设置的iPad初始取向在横向模式


    解决方案

    与iPad的问题是,这两个方向是psented定期重新$ P $。

    问题的解决方法是两个IBOutlet中集合添加到您希望这个方向变化发生的视图控制器,例如:

      @IBOutlet VAR landscapeConstraints:[NSLayoutConstraint]!
    @IBOutlet VAR portraitConstraints:[NSLayoutConstraint]!

    转至故事板,切换到你想从每个活动约束portraitConstraints IBOutlet中iPad的纵向和控制拖发生的适应性布局。
    更改Adaptive布局到要发生的景观,并再次控制从刚才的有效约束拖动landscapeConstraints IBOutlet中的人。

    在视图控制器控 viewWillTransitionToSize 方法:

     覆盖FUNC viewWillTransitionToSize(尺寸:CGSize,withTransitionCoordinator协调员:UIViewControllerTransitionCoordinator){
            super.viewWillTransitionToSize(大小,withTransitionCoordinator:协调人)
            让transitionToLandscape = size.width&GT; size.height
            让constraintsToUninstall = transitionToLandscape? ortraitConstraints:landscapeConstraints
            让constraintsToInstall = transitionToLandscape? landscapeConstraints:portraitConstraints        view.layoutIfNeeded()
            coordinator.animateAlongsideTransition({
                在_
                NSLayoutConstraint.deactivateConstraints(constraintsToUninstall)
                NSLayoutConstraint.activateConstraints(constraintsToInstall)
                self.view.layoutIfNeeded()
            }完成:无)
    }

    //扩大,制约例如:

    转到任意大小的任何阶级和看到约束他们中的一些会被gayed出来,这意味着在这里不活跃,但将在不同尺寸的类是积极的,例如任何正规。

    //扩展

    要设置的初始方向,你可以重写viewWillAppear中和安装/卸载权的限制:

     覆盖FUNC viewWillAppear中(动画:BOOL){
            super.viewWillAppear(动画)        让transitionToLandscape = view.frame.size.width&GT; view.frame.size.height        让constraintsToUninstall = transitionToLandscape? portraitConstraints:landscapeConstraints
            让constraintsToInstall = transitionToLandscape? landscapeConstraints:portraitConstraints        view.layoutIfNeeded()
            NSLayoutConstraint.deactivateConstraints(constraintsToUninstall)
            NSLayoutConstraint.activateConstraints(constraintsToInstall)
        }

    I am following this article for building adaptive layout in ios Building Adaptive User Interfaces for iOS 8.

    It will work fine on iPhones . Now i wan't to give the same effect on iPad. But for iPad adaptive layout is not work.
    Here is screenshot of application in iPhone5 (green box represent view1 and yellow box represent view2)
    1.Portrait mode

    1. Landscape mode

    Question: how can achieve same effect for iPad?
    Expanded Question: How to set up initial orientation of iPad in landscape mode?

    解决方案

    The problem with the iPad is that both orientation is represented as Regular.

    One of the solution is to add two IBOutlet collection to your view controller where you want this orientation change to happened, for example:

    @IBOutlet var landscapeConstraints: [NSLayoutConstraint]!
    @IBOutlet var portraitConstraints: [NSLayoutConstraint]!
    

    Go to the storyboard, switch to the Adaptive Layout you want to happened in iPad portrait orientation and control drag from every ACTIVE constraints to portraitConstraints IBOutlet. Change the Adaptive Layout to the one you want to happened for landscape and again control drag from just ACTIVE constraints to landscapeConstraints IBOutlet.

    In view controller override viewWillTransitionToSize method:

        override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
            super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
            let transitionToLandscape = size.width > size.height 
            let constraintsToUninstall = transitionToLandscape ? ortraitConstraints : landscapeConstraints 
            let constraintsToInstall = transitionToLandscape ? landscapeConstraints : portraitConstraints
    
            view.layoutIfNeeded()
            coordinator.animateAlongsideTransition({
                _ in
                NSLayoutConstraint.deactivateConstraints(constraintsToUninstall) 
                NSLayoutConstraint.activateConstraints(constraintsToInstall) 
                self.view.layoutIfNeeded()
            }, completion: nil)
    }
    

    // Expanded, constraints example:

    Go to any-any size class and see the constraint some of them will be gayed out which means that are not active here but will be active in different size classes, for example any-regular.

    // Expanded

    To set up initial orientation you can override viewWillAppear and install/uninstall the right constraints:

    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
    
            let transitionToLandscape = view.frame.size.width > view.frame.size.height
    
            let constraintsToUninstall = transitionToLandscape ? portraitConstraints : landscapeConstraints
            let constraintsToInstall = transitionToLandscape ? landscapeConstraints : portraitConstraints
    
            view.layoutIfNeeded()
            NSLayoutConstraint.deactivateConstraints(constraintsToUninstall)
            NSLayoutConstraint.activateConstraints(constraintsToInstall)
        }
    

    这篇关于自适应布局为iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆