在旋转时放置和移动视图 (UIView) [英] Placing and Moving Views on Rotation (UIView)

查看:21
本文介绍了在旋转时放置和移动视图 (UIView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用旋转时准确放置和移动视图的正确"方式是什么?也就是说,当 UI 从纵向旋转到横向(反之亦然)时,如何对视图的位置、大小和回流进行细粒度控制?我认为我的两个选择是:

What's the "correct" way of exactly placing and moving views when an app rotates? That is, how can I have fine-grained control of the position, size, and reflow of my views when the UI rotates from portrait to landscape orientation (or vice-versa)? I think my two options are:

  1. 使用两个超级视图(纵向和横向).旋转时:在它们之间切换.
  2. 使用一个超级视图.旋转时:更改每个子视图的框架、边界和中心属性.

如果您有两个具有足够不同布局和元素的视图,那么第一种方法可能就足够了.如果您的两个视图在不同方向的大小基本相同,那么第二种方法可能是仅使用一个视图的更好方法.

If you have two views with distinct enough layouts and elements, then the first way might be good enough. If your two views are essentially the same thing sized for different orientations, the second way is probably a better way to do it using only one view.

我怀疑前者可以用 IB 来完成,后者应该通过编程来完成.

I suspect the former could be done with IB and the latter should be done programmatically.

推荐答案

要在 iPhone 旋转时对子视图的位置和大小进行细粒度控制,请在 UIViewController 方法中更改子视图的框架 willAnimateRotationToInterfaceOrientation:持续时间:.此方法在动画块内调用,因此您在其中对子视图的帧所做的所有更改都是动画的.

To have fine-grained control over the position and size of your subviews when the iPhone rotates, change the frame of the subviews in the UIViewController method willAnimateRotationToInterfaceOrientation:duration:. This method is called within an animation block, so all the changes to your subviews' frames that you make inside of it are animated.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        // Portrait frames
        self.subviewA.frame = CGRectMake(x, y, width, height);
        self.subviewB.frame = CGRectMake(x, y, width, height);
        self.subviewC.frame = CGRectMake(x, y, width, height);
    } else {
        // Landscape frames
        self.subviewA.frame = CGRectMake(x, y, width, height);
        self.subviewB.frame = CGRectMake(x, y, width, height);
        self.subviewC.frame = CGRectMake(x, y, width, height);
    }
}

这篇关于在旋转时放置和移动视图 (UIView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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