开发者

Getting the correct frame inside willRotateToInterfaceOrientation

My subviews have different layout when in portrait and landscape orientation. The layout is based on the frame of the superview, and is not trivial (can't be achieved using autoresizing springs and struts).

The problem is that if i'm changing the frame of the subViews during willRotateToInterfaceOrientation, th开发者_JS百科e animation is correct, but the frame is the superView is still the frame from the old orientation.

If i'm changing the subViews during didRotateFromInterfaceOrientation, the frame is the superView is indeed the correct one, but the frame change only occurs after the rotating animation and it doesn't look good.

What is the correct way to change the frames of the subViews when changing orientation?


This function works for what you need because the views frame has changed from the rotation and it doesn't stall the custom animation.

-(void)willAnimateRotationToInterfaceOrientation (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    float width = UIInterfaceOrientationIsPortrait(toInterfaceOrientation) ? [[UIScreen mainScreen] applicationFrame].size.width : [[UIScreen mainScreen] applicationFrame].size.height;
//do something with the width now
//or you can get the height the same way
}


I think you can try overriding layoutSubviews. There you can layout the subviews in accordance with the current orientation, without bothering about will/did RotateFromInterfaceOrientation. layoutSubviews is called every time the views are laid out. This shall help you in timing your frame changes with the animation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜