【问题标题】:How to create a static subview with navigation controller?如何使用导航控制器创建静态子视图?
【发布时间】:2012-05-03 15:06:13
【问题描述】:

我创建了一个带有一堆视图控制器的导航控制器。

我想在底部添加一个子视图,当用户在这堆视图之间导航时它保持静态(不移动)。

与某些应用一样,底部有一个“广告栏”。

我该怎么做?

【问题讨论】:

    标签: ios xcode uiview uinavigationcontroller three20


    【解决方案1】:

    如果我理解正确,这就是你想要的:

    您可以通过创建一个包含 UINavigationController 的自定义 UIViewController 来实现这一点。创建一个名为“CustomViewController”的新类,并粘贴以下代码:

    界面

    #import <UIKit/UIKit.h>
    
    @interface CustomViewController : UIViewController
    
    - (id)initWithViewController:(UIViewController*)viewController bottomView:(UIView*)bottomView;
    
    @end
    

    实施:

    #import "CustomViewController.h"
    
    @implementation CustomViewController
    
    - (id)initWithViewController:(UIViewController*)viewController bottomView:(UIView*)bottomView
    {
        self = [super init];
        if (self) {
    
            //  Set up view size for navigationController; use full bounds minus 60pt at the bottom
            CGRect navigationControllerFrame = self.view.bounds;
            navigationControllerFrame.size.height -= 60;
            viewController.view.frame = navigationControllerFrame;
    
            //  Set up view size for bottomView
            CGRect bottomViewFrame = CGRectMake(0, self.view.bounds.size.height-60, self.view.bounds.size.width, 60);
            bottomView.frame = bottomViewFrame;
    
            //  Enable autoresizing for both the navigationController and the bottomView
            viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            bottomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
            //  Add views as subviews to the current view
            [self.view addSubview:viewController.view];
            [self.view addSubview:bottomView];
        }
        return self;
    }
    
    @end
    

    现在使用 CustomViewController:

    UIView *myBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    myBottomView.backgroundColor = [UIColor redColor];
    
    CustomViewController *customViewController = [[CustomViewController alloc] initWithViewController:<yourNavigationController> bottomView:myView];
    

    【讨论】:

    • 哦,,,我实现的时候有问题,我可以得到一个实现的源代码吗?太好了:D
    • @PangHoMing 如果您告诉我更多有关如何设置 UINavigationController 的信息,我可以提供更多代码/信息。
    • 我的应用设置是... StartPage(Launcher Page)----modal transition-->View1----(navigation Push)---->View2----(再次推)--->View3 ........我想做的是在view2和view3的底部有一个所谓的固定UIView ......我已经按照你的指示创建了在我的 View2.m 的 .m 中导入“CustomeViewController 类”,然后分配初始化 CustomerViewController *customViewController 然后我不知道下一步该怎么做|||
    • @PangHoMing 我认为如果您在 AppDelegate 中初始化 CustomViewController 将是最简单的。很难提供更多建议,因为我不知道您是如何创建项目的(在哪里使用 xib 文件等)。你认为你可以把你的代码发给我吗?
    • @AndreasLey 你能为此整理一个小型演示项目吗?我的问题是您使用导航控制器的视图作为自定义视图控制器视图的子视图。我正在尝试找到一种解决方案来制作类似的东西,但到目前为止我不能使用这样的 UINavigationController。虽然我同意从理论上讲,您的架构/设置在逻辑上是这些人解决他的问题所需要的。
    【解决方案2】:

    为什么不将此静态子视图添加到与导航控制器视图相同的级别(通常在 UIWindow 附近)。只需将其添加到最上面即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多