gpt4 book ai didi

ios - Storyboard在不同时间显示多个 View

转载 作者:行者123 更新时间:2023-11-29 13:03:57 24 4
gpt4 key购买 nike

我有一个应用程序,它应该根据加速度计报告的值显示一组不同 View 中的一个。我想在 Xcode 的 Storyboard Editor 中设置 View ,née Interface Builder。此外,我想使用 Xcode 的新式自动布局工具。基本上,这些工具允许您定义对象间约束,并且运行时系统会计算出所述对象的去向。

这是我想要的伪代码的简单示例:

if (accelerometer == toTheLeft) {
[topLevelView showSubview:leftView];
} else if (accelerometer == toTheRight) {
[topLevelView showSubview:rightView];
} else {
[topLevelView showSubview:centerView];
}

我知道要执行虚构的 showSubview: 方法,我实际上必须删除当前显示的任何不正确的 View ,并添加我想要的 View ,但这不是这里的重点。

如前所述,我想对每个 UIView 对象(即 leftViewrightViewcenterView) 使用 Storyboard Editor/Interace Builder。但是,当我这样做时,我最终会在 IB 中看到相互堆叠的不同 View 的困惑局面。我的 View Controller 最终会在其 View 中添加和删除 View ,但是通过这种方式设置自动布局非常困难。

最后是我的问题。 有没有更好的方法可以在 Storyboard编辑器中为一个 View Controller 直接排列许多经常重叠的 View ,或者我只需要以编程方式设置所有内容?


我的内省(introspection)常见问题解答,供热心读者使用:

  1. 您为什么对 Storyboard Editor/IB 如此着迷?它可以自动找出 View 定位和调整大小的许多痛苦部分。听起来不错!如果可能的话,我想让它为我工作。

  2. 以前肯定有人问过这个问题,对吧? 呃,差不多吧。 This question在我发现的那些中最接近。然而,这个解决方案仍然需要我以编程方式设置框架,否定了在我看来是使用 IB 的最佳理由——自动布局。 This one也让我兴奋,但实际上不是一回事。

最佳答案

我在我正在处理的应用程序中遇到了类似的情况。我所做的是向我的主视图 Controller 添加了两个容器 View (尽管你听起来好像只需要一个)。我删除了容器中的嵌入式 View ,并将它们作为 IBOutlets 添加到我的头文件中:

@property (weak, nonatomic) IBOutlet UIView *topContainerView;
@property (weak, nonatomic) IBOutlet UIView *bottomContainerView;

然后我继续为我想使用的三个 subview 中的每一个创建 UIViewControllers,然后我可以通过 Storyboard进行设计。

在我的实现文件中,我还有两个属性,

@interface WorkOrderContainerViewController ()
@property UIViewController *topViewController;
@property UIViewController *bottomViewController;
@end

为了在显示的 View Controller 之间切换,我使用了以下方法(底部容器 View 也有两个类似的方法):

- (void)presentTopViewController:(UIViewController *)viewController
{
// Remove current top view controller.
if (topViewController != nil) {
[self removeTopViewController];
}

[self addChildViewController:viewController];
[[viewController view] setFrame:[topContainerView bounds]];
[topContainerView addSubview:[viewController view]];
[self setTopViewController:viewController];
[viewController didMoveToParentViewController:self];
}

- (void)removeTopViewController
{
[topViewController willMoveToParentViewController:nil];
[[topViewController view] removeFromSuperview];
[topViewController removeFromParentViewController];
}

然后我可以简单地实例化我的 View

[self presentTopViewController:[storyboard instantiateViewControllerWithIdentifier:@"leftView"]];

我知道您正在寻找一种管理重叠 View 的方法,但将每个 View 作为单独的 View Controller 并以这种方式执行可能会更容易。

关于ios - Storyboard在不同时间显示多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19060089/

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