旋转UIView时覆盖layoutSubviews [英] Overriding layoutSubviews when rotating UIView

查看:124
本文介绍了旋转UIView时覆盖layoutSubviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UIView内旋转滚动视图时,滚动视图无法使用其默认的自动调整行为正确定位。

When rotating a scroll view inside a UIView, the scroll view doesnt position correctly using its default autoresizing behaviour.

因此,当轮换发生时,在 willRotateToInterfaceOrientation 我调用 [self.view setNeedsLayout] ; 和我的 layoutSubviews 方法如下:

Thus, when rotation occurs, in willRotateToInterfaceOrientation i call [self.view setNeedsLayout]; and my layoutSubviews method is as follows:

- (void)layoutSubviews {
    NSLog(@"here in layoutSubviews");
}

但是在方法上设置一个断点,它似乎永远不会进入方法。

But putting a breakpoint on the method, and it never seems to enter the method.

我是否需要做其他事情才能让它发挥作用?

Do i need to do something else to get it to work?

谢谢。

推荐答案

在更改方向之前调用willRotateToInterfaceOrientation,因此您的UIView仍将具有旧的大小。
尝试使用didRotateFromInterfaceOrientation。

willRotateToInterfaceOrientation is being called before the orientation is changed, hence your UIView will still have the old size. Try using didRotateFromInterfaceOrientation instead.

另外,为了增加效果,我会在willRotateToInterfaceOrientation中隐藏scrollView(可能在UIAnimation块内),调整大小然后再调整它在didRotateFromInterfaceOrientation中显示它(再次,可能在动画块内)。

Also, for added effect, I would hide the scrollView ( maybe with inside an UIAnimation block) in willRotateToInterfaceOrientation, resize it and then show it in didRotateFromInterfaceOrientation (again, maybe inside an animation block).

这是我的一个应用程序的片段:

Here is a snippet from one of my apps:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myScroll.hidden = YES;
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myScroll.hidden = NO;
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}

您甚至可以通过使用UIInterfaceOrientationIsPortrait(方向)检查新方向来做一些奇特的事情或UIInterfaceOrientationIsLandscape(方向)。

You can even do fancy stuff by checking the new orientation using UIInterfaceOrientationIsPortrait(orientation) or UIInterfaceOrientationIsLandscape(orientation).

这篇关于旋转UIView时覆盖layoutSubviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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