【问题标题】:UIView rearranging on rotationUIView 在旋转时重新排列
【发布时间】:2013-12-03 13:02:44
【问题描述】:

我正在为该项目使用情节提要。 我有一个带有 3 个按钮的顶部菜单(UIView,其中包含 3 个UIButton),它们之间的距离相同。

我想在旋转时将此菜单拉伸到最大宽度并增加按钮之间的距离。

我做了以下事情:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)orientation {
    [super didRotateFromInterfaceOrientation:orientation];
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation)){
        [self prepareLandscapeLayout];
    }
}

- (void) prepareLandscapeLayout {   
    [[self topMenu] setFrame:CGRectMake(0, 0, IS_IPHONE_5 ? 568 : 480, 59)];
    [[self cameraButton] setCenter:CGPointMake(232, 26)];
    [[self settingsButton] setCenter:CGPointMake(288, 26)];
    [[self videoList] setCenter:CGPointMake(340, 26)];
}

prepareLandscapeLayout 函数在旋转后被调用,但按钮保持在原来的位置。我还可以看到topMenu 在旋转后如何拉伸(我已经更改了背景颜色)。

对我可能做错的事情有什么想法吗?

【问题讨论】:

  • 设置按钮的中心不会拉伸它,它只会移动它。像使用顶部菜单一样更改按钮的实际框架
  • @danypata 我不想调整它们的大小,我只需要将它们放到新位置

标签: ios objective-c layout


【解决方案1】:

中心是相对于它的原始方向并且是从框架计算出来的(也相对于原始方向)。

相反,尝试将框架更改为相对于父视图的边界(根据方向更新)。

【讨论】:

  • 如果我将 setCenter 更改为 setFrame 没有任何变化。
  • 更换框架时,使用topMenu.bounds代替topMenu.frame。
【解决方案2】:

如果 topMenu 的框架发生变化,那么就这样写。

- (void) prepareLandscapeLayout 
{   
[[self topMenu] setFrame:CGRectMake(0, 0, IS_IPHONE_5 ? 568 : 480, 59)];
[[self cameraButton] setFrame:CGRectMake(0,0,actualWidth,actualHeight)];
[[self settingsButton] setFrame:CGRectMake(topMenu.frame.size.width/3,0,actualWidth,actualHeight)];
[[self videoList] setFrame:CGRectMake((topMenu.frame.size.width/3)*2,0,actualWidth,actualHeight)];
}

【讨论】:

  • topMenu 的框架是否在改变方向?
  • 好吧,如果事实上它随手机旋转,但仍停留在手机的当前上部(它看起来像 safari 横向模式下的 url 栏)并延伸到手机的整个宽度。
【解决方案3】:

只要您不想使用故事板设计进行旋转和约束,您就应该停止使用“自动布局”

在故事板右窗格的任何控制器中查看Interface Builder Document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多