具有容器视图的iPad纵向和横向模式的不同大小类 [英] Different size classes for iPad portrait and landscape modes with containerviews

查看:211
本文介绍了具有容器视图的iPad纵向和横向模式的不同大小类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现



当我不分配rootviewcontroller时,它看起来像这样



虽然它在纵向模式下看起来像这样但是



有谁知道这里可能出现的问题?为什么它不会像所希望的那样改变大小等级。



编辑
像@Muhammad Yawar Ali这里问的截图来自我设置的所有大小类的位置。我没有任何约束的警告或错误,因此这些屏幕截图包含更新的视图。






我希望这能显示所需的一切。



编辑:
由于某些原因我无法输入所有截图

解决方案

viewWillTransitionToSize 你还需要调用super,将事件传递给下一个响应者(你的rootviewcontroller)......

 覆盖func viewWillTransitionToSize(size:CGSize,withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator){
super.viewWillTransitionToSize(size,withTransit ionCoordinator:coordinator)
willTransitionToPortrait = size.height> size.width
}


I've found this question and I've tried to implement the solution that has been given. However I run into a problem.

My initial view controller has two container views who both have their own view controller. I've created a root view controller that is assigned to the initial view controller. The code in this class looks like this.

class RootViewController: UIViewController {

var willTransitionToPortrait: Bool!
var traitCollection_CompactRegular: UITraitCollection!
var traitCollection_AnyAny: UITraitCollection!

override func viewDidLoad() {
    super.viewDidLoad()
    setupReferenceSizeClasses()
    // Do any additional setup after loading the view.
}

override func viewWillAppear(animated: Bool) {
    willTransitionToPortrait = self.view.frame.size.height > self.view.frame.size.width
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    willTransitionToPortrait = size.height > size.width
}

func setupReferenceSizeClasses(){
    let traitCollection_hCompact = UITraitCollection(horizontalSizeClass: .Compact)
    let traitCollection_vRegular = UITraitCollection(verticalSizeClass: .Regular)
    traitCollection_CompactRegular = UITraitCollection(traitsFromCollections: [traitCollection_hCompact, traitCollection_vRegular])

    let traitCollection_hAny = UITraitCollection(horizontalSizeClass: .Unspecified)
    let traitCollection_vAny = UITraitCollection(verticalSizeClass: .Unspecified)
    traitCollection_AnyAny = UITraitCollection(traitsFromCollections: [traitCollection_hAny, traitCollection_vAny])
}

override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {
    let traitCollectionForOverride = ((willTransitionToPortrait) != nil) ? traitCollection_CompactRegular : traitCollection_AnyAny

    return traitCollectionForOverride;
}

However when I run it the size class won't respons like it should. One of the container view controllers will start acting weird in both landscape and portrait mode like can be seen below.

When I don't assign the rootviewcontroller it will look like this

While it should look like this in portrait mode

Does anyone know what might be going wrong here? Why it doesn't change the size class like desired.

EDIT Like @Muhammad Yawar Ali asked here are screenshots from the position of all the size classes I've set. I have no warnings or errors on any constraints so these screenshots contain the updated views.

I hope this shows everything that is needed.

EDIT: for some reason I'm unable to put in all the screenshots

解决方案

On the viewWillTransitionToSize you need to call also super, to pass the event to the next responder (your rootviewcontroller)...

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    willTransitionToPortrait = size.height > size.width
}

这篇关于具有容器视图的iPad纵向和横向模式的不同大小类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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