gpt4 book ai didi

ios - AutoLayout:垂直到水平没有明确的尺寸?

转载 作者:行者123 更新时间:2023-11-29 02:45:49 25 4
gpt4 key购买 nike

我对完成这类事情很感兴趣,最好只使用 AutoLayout 而不要使用代码:

Demo 1

以下是我需要维护的约束:

  • 没有明确的 View 尺寸,因为应该支持 iPhone 和 iPad。
  • View 1 保持其方形纵横比。
  • View 2 占用所有剩余空间(垂直或水平)并调整其 subview 的大小。
  • 旋转时,从垂直对齐更改为水平对齐。

对于如何完成这样的事情,我将不胜感激。 AutoLayout 对于这类任务似乎有点困惑。

非常感谢!

附言- 作为一个更复杂的后续行动,你认为这样的事情是可以实现的吗?我有点怀疑。

Demo 2

最佳答案

所有这一切都可以通过自动布局实现。它只需要一些代码!

要解决每个要点:

  • 这就是 AutoLayout 的意义所在。根据您的实现,您实际上应该很难让它同时在 iPad 和 iPhone 上运行。

  • 没问题

    [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
  • 当然可以

    UIView *view1;
    UIView *view2;
    UIView *superview;
    NSDictionary *metrics = @{@"border":@(VIEW1_BORDER)};
    NSDictionary *views = NSDictionaryOfVariableBindings(view1,view2);
    UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
    if (UIDeviceOrientationIsPortrait(orientation)) {
    [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view1]-(border)-[view2]|" options:0 metrics:metrics views:views]];
    [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view2]|" options:0 metrics:nil views:views]];
    } else {
    [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view1]-(border)-[view2]|" options:0 metrics:metrics views:views]];
    [superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view2]|" options:0 metrics:nil views:views]];
    }
  • 实现 UIViewController willRotateToInterfaceOrientation:duration: 方法并使用上面的相同代码。请务必在添加新约束之前删除任何先前添加的约束,以避免约束冲突。 更新:由于 willRotateToInterfaceOrientation:duration: 在 iOS 8 中已弃用,您可以观察默认的 NSNotificationCenterUIApplicationWillChangeStatusBarOrientationNotification 和UIApplicationDidChangeStatusBarOrientationNotification处理方向变化的通知。

使用三个 View 与上面的代码几乎完全相同。

关于ios - AutoLayout:垂直到水平没有明确的尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25150118/

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