本文介绍了在旋转上放置和移动视图(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:duration:。此方法在动画块中调用,因此您在其中制作的子视图帧的所有更改都是动画的。

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:38