gpt4 book ai didi

iphone - 在旋转时放置和移动 View (UIView)

转载 作者:IT王子 更新时间:2023-10-29 08:20:29 24 4
gpt4 key购买 nike

当应用程序旋转时,准确放置和移动 View 的“正确”方法是什么?也就是说,当 UI 从纵向旋转到横向(或反之亦然)时,我如何才能对 View 的位置、大小和重排进行细粒度控制?我认为我的两个选择是:

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

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

我怀疑前者可以用 IB 完成,而后者应该以编程方式完成。

portrait to landscape

最佳答案

要在 iPhone 旋转时对 subview 的位置和大小进行细粒度控制,请在 UIViewController 方法 willAnimateRotationToInterfaceOrientation:duration: 中更改 subview 的框架。此方法在动画 block 中调用,因此您在其中对 subview 的帧所做的所有更改都是动画的。

- (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);
}
}

关于iphone - 在旋转时放置和移动 View (UIView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6104963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com