根据大小分类的UIStackView属性 [英] UIStackView properties according to size classes

查看:75
本文介绍了根据大小分类的UIStackView属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在情节提要中测试UIStackView,然后根据iPhone的方向(wAny/hC,wC/hR)更改属性"Axis".

I try to test the UIStackView in a storyboard where I change the property "Axis" based on the orientation of an iPhone (wAny/hC, wC/hR).

不幸的是,在运行时,未应用更改. 具体来说,修改了轴"属性(在Xcode中进行了检查),但是布局不会更改.

Unfortunately, at running, the change is not applied. Specifically, the property "Axis" is modified (inspected in Xcode), but the layout will not change.

预先感谢您的帮助.

推荐答案

这绝对是一个错误.仔细检查后,轴在运行时(模拟器或设备)的行为仅会针对视图层次结构中的每个其他 UIStackView进行更改.

This is definitely a bug. The behavior of the axis at runtime (simulator or device), after closer inspection, only changes for every other UIStackView in the view hierarchy.

我并没有尝试所有可能的排列方式,但这是我发现的问题,因为只有在我想引入多个具有此所需轴更改行为的堆栈视图后,这个问题才会出现.在某些情况下,单个堆栈视图可能不遵循大小类规则,但是我还没有可靠地复制它.

I haven't tried every possible permutation, but this is what I am finding since the issue only cropped up once I wanted to introduce more than one stack view with this desired axis-changing behavior. There may be instances where a single stack view does not obey the size class rule, but I haven't reliably reproduced it.

查看我的 iOS 9.1项目,其中使用垂直排列的8个UIStackView元素清楚地展示了此问题. .它们的配置都完全,除了它们相对于彼此的位置以及屏幕顶部受到约束之外.

Check out my iOS 9.1 project which clearly demonstrates this problem with 8 UIStackView elements in a vertical arrangement. They are all configured exactly the same other than their position constrained relative to each other and the top of the screen.

从情节提要预览中可以看到,事物在风景中看起来像我们想要的和期望的一样:

As you can see from the storyboard preview, things look as we want and expect in landscape:

在模拟器或设备上运行时,情况就不同了.在尝试了许多堆栈视图之后,很明显该错误会影响视图层次结构中的每个 other 堆栈视图.

It becomes a different story when running on the simulator or a device. After experimenting with many stack views, it becomes apparent that the bug affects every other stack view in the view hierarchy.

尽管在上面的评论中听到了另一个提及,但我在此示例项目中提出了自己的RDAR.我找不到它的公开记录.

I have raised my own separate RDAR with this example project, despite hearing mention of another in the comments above. I could not find a public record of it.

显然,有一个基于代码的解决方法很痛苦,完全违背了自动布局,约束和大小类的目的.对我来说,这是一个非常重要的错误,因为它消除了堆栈视图和大小类的一个非常重要的用例.

Apparently there is a code-based workaround that is painful and totally against the purpose of autolayout, constraints, and size classes. This is a pretty major bug to me as it eliminates a pretty major use case for stack views and size classes.

根据EinharchAltPlus的帖子在苹果开发者论坛中的解决方法:

The code workaround according to EinharchAltPlus's post in Apple Developer forums:

顺便说一句:重写viewWillTransitionToSize并手动设置不同的参数可以解决此问题,但这非常累人,因为我们还需要在代码中检查视图,这应该纯粹与设计相关.

BTW: overriding viewWillTransitionToSize and manually setting different parameters solves the issue, but this is pretty tiresome as we need to check view in code as well, something that should be purely design related.

如果还不清楚,您需要对非合作堆栈视图有一个IBOutlet引用,并且该视图可能会填充您的视图控制器代码中.您可以更改堆栈视图的axis属性,最好更改其包含在其中的高度:

In case that's not entirely clear, you need to have a IBOutlet reference to the non-cooperative stack view and perhaps the view it fills in your view controller code. You can change the stack view's axis property and, ideally, the height of what it's contained in:

@IBOutlet weak var viewHackStack: UIView!
@IBOutlet weak var hackStackView: UIStackView!

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

    if(self.traitCollection.verticalSizeClass == .Compact) {
        self.viewHackStack.frame.size.height = 25
        self.hackStackView.axis = .Horizontal
    }
    else if(self.traitCollection.verticalSizeClass == .Regular) {
        self.viewHackStack.frame.size.height = 50
        self.hackStackView.axis = .Vertical
    }
}

如果您想更具体地确定决定行为改变的大小等级,请尝试使用特征收集大小等级类型,直到满足所需条件为止.

If you want to be more specific with the size class that determines the behavior change, just play around with the trait collection size class types until you've met your desired condition.

如果我在错误修复时间轴上听到任何声音,我将在此处发布.在此之前,我希望这可以作为解决方法.

If I hear anything back on a bug fix timeline, I will post it here. Until then, I hope this helps as a workaround.

这篇关于根据大小分类的UIStackView属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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