使按钮在所有视图控制器上保持不变 [英] Making a button persistent across all view controllers

查看:112
本文介绍了使按钮在所有视图控制器上保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序的右下角有一个持久性按钮。在所有视图过渡期间,按钮应保持静态。我在确定添加按钮的视图时遇到问题。我知道按钮应该存储在AppDelegate中,但我不知道除了窗口之外添加它的其他视图是什么意思。将其添加到窗口的一个缺点是,当在后台运行应用程序(即电话)时,添加的状态栏填充将按下窗口。一般来说,将它添加到窗口似乎是一个hacky解决方案 - 任何想法?

I want to have a persistent button in the bottom right corner of my app. During all view transitions, the button should remain static. I'm having trouble deciding what view to add the button to. I know the button ought to be stored in the AppDelegate, but I don't know what other view it would be sense to add it to except the window. One downside of adding it to the window is that when there's an app running in the background (ie Phone), the added status bar padding will push down the window. In general, adding it to the window seems to be a hacky solution -- any thoughts?

推荐答案

是的,将它添加到UIWindow非常hacky和挑剔。

Yes, adding it to the UIWindow would be extremely hacky and finicky.

如果您使用的是Storyboard和iOS 5.0,您应该能够使用容器视图并执行以下操作:

If you're using Storyboards and iOS 5.0 onwards, you should be able to use container views and do something like this:

这是另一张图片,展示了第一个View Controller的相当简单的结构:

Here's another picture showing the, rather simplistic, structure of the first View Controller:

左侧的视图控制器有一个容器,然后是一个视图,它将按钮放在它上面。容器指示导航控制器(直接向右)应出现在其自身内,该关系由 =([])=> 箭头显示(正式名称为一个嵌入segue )。最后,导航控制器将其根视图控制器定义为右侧的控制器。

The view controller on the left has a container, and then a view which holds the button on top of it. The container indicates that the navigation controller (directly to the right) should appear within itself, that relationship is shown by the =([])=> arrow (formally known as an embed segue). Finally the navigation controller defines its root view controller to the one on the right.

总之,第一个视图控制器在容器视图中煎饼,顶部有按钮,所以内部发生的一切必须让按钮位于顶部。

In summary, the first view controller pancakes-in the container view with the button on top, so everything that happens inside has to have the button on top.

使用与Storyboard版本类似的结构,您可以使用其按钮创建基本视图控制器,然后添加视图这将成为应用程序的新根,在下面。

Using a similar structure to the Storyboard version, you could create the base view controller with its button, and then, add the view that will become then new "root" of the application, underneath.

为了清楚,让我们调用一个持有按钮的视图控制器 FakeRootViewController ,以及视图控制器,出于所有实际目的,它将是应用程序的根目录: RootViewController 。所有后续视图控制器甚至都不会知道 FakeRootViewController 高于其他所有人。

To make it clear, let's call the one view controller that holds the button the FakeRootViewController, and the view controller that will be, for all practical purposes, the root of the application: RootViewController. All subsequent view controllers won't even know that there's the FakeRootViewController above everyone else.

// The "real" root
#import "RootViewController.h"

// Call once after the view has been set up (either through nib or coded).
- (void)setupRootViewController
{
    // Instantiate what will become the new root
    RootViewController *root = [[RootViewController alloc] <#initWith...#>];

    // Create the Navigation Controller
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];

    // Add its view beneath all ours (including the button we made)
    [self addChildViewController:nav];
    [self.view insertSubview:nav.view atIndex:0];
    [nav didMoveToParentViewController:self];
}



AppDelegate.m



AppDelegate.m

#import "FakeRootViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    FakeRootViewController *fakeRoot = [[FakeRootViewController alloc] <#initWith...#>];

    self.window.rootViewController = fakeRoot;
    [self.window makeKeyAndVisible];

    return YES;
}

这样,您可以享受在窗口上插入按钮的所有好处没有任何负罪感和我真的应该成为一名程序员吗?它会导致。

That way, you can have all the benefits of inserting the button on the window, without all the guilt and "Should I really be a programmer?" that it causes.

这篇关于使按钮在所有视图控制器上保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆