自动版式和调整的UILabel动画无法在设备旋转工作正常 [英] AutoLayout and UILabel resize animation not working properly on device rotation

查看:126
本文介绍了自动版式和调整的UILabel动画无法在设备旋转工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的任务,一个奇怪的问题。

i have a strange problem on a very simple task.

我需要一个非常小的设置来重现问题:

I needs a very small setup to reproduce the problem:

|--------------------------------------|
|             Parent View              |
|                                      |
||------------------------------------||
||              UILabel               ||
||------------------------------------||
|                                      |
|                                      |
||------------------------------------||
||              UIView                ||
||------------------------------------||
|                                      |
|                                      |
|--------------------------------------|

在上面的例子中,我们有2子视图父视图 - >一个UILabel和一个简单的UIView。使用自动布局应用上面显示的布局IM:

In the example above we have a parent view with 2 subviews --> A UILabel and a simple UIView. Im using autolayout to apply the layout shown above:

Constraints for UILabel:
Leading Space to superview = 0
Trailing Space to superview = 0
Fixed Height constraint (e.g. 80pt)
Top Space constraint (e.g. 50pt)

The UIView subview has the same constraint types (the values for height and top space differs).

所以,我期待着与此设置,是两个子视图将通过其母公司的全宽为我们定义的开头和结尾的空间应该是零。因此,如果更改父视图的宽度,子视图的宽度应改变太多,要保持对齐。

So what i expect with this setup, is that both subviews will adopt the full width of its parent as we defined that leading and trailing space should be zero. so if change the width of the parent view, the width of the subviews should change too, to stay aligned.

可以说父视图具有200x400 PT的维度。当我建立和运行人像我的例子一切看起来正常。当我旋转为横向,一切看起来还是很好。家长意见宽度甚至子视图的宽度得到了较大。但现在,当我转回到肖像uilabels宽度立即获取其目标宽度没有任何动画:

lets say the parent view has a dimension of 200x400 pt. when i build and run my example in portrait everything looks ok. when i rotate to landscape, everything still looks fine. the parent views width and even the subviews width got larger. but now when i rotate back to portrait the uilabels width immediately gets its target width without any animation:

|--------------------------------------|
|             Parent View              |
|                                      |
|    (immediately has target size)     |
|          |--------------|            |
|          |    UILabel   |            |
|          |--------------|            |
|                                      |
|   (this subview is still             |
|             animating its width)     |
|   |------------------------------|   |
|-->|           UIView             |<--|
|   |------------------------------|   |
|                                      |
|                                      |
|--------------------------------------|

意见宽度父和子视图的UIView宽度正确动画。其唯一的,这里有一个奇怪的行为的UILabel,我不能找出是什么原因导致这个问题。

the parent views width and the uiview subviews width are animated properly. its only the uilabel that has a strange behavior here and i cant find out what causes that issue.

推荐答案

我的建议是:

如果你的子类的父视图使用 - (空)layoutSubviews

If you've subclassed the parent view use the - (void)layoutSubviews:

- (void)layoutSubviews {
    label.frame = CGRectMake(leftInset, labelY, parentView.frame.size.width - leftInset*2, labelHeight);
    childView.frame = CGRectMake(leftInset, childViewY, parentView.frame.size.width - leftInset*2, childHeight);
}

如果你没有子类可以使用父视图 autoresizingMask

if you didn't subclassed the parent view you can use autoresizingMask:

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
childView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

这篇关于自动版式和调整的UILabel动画无法在设备旋转工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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