如何创建带有导航控制器的静态子视图?

3
我已经创建了一个带有视图控制器堆栈的导航控制器。
我想在底部添加一个子视图,该子视图在用户在这个视图堆栈之间导航时保持静态(不移动)。
就像一些应用程序中,在底部有一个“广告栏”。
我该如何实现这个功能?
2个回答

13

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

UIView below UINavigationController or below TabBar

你可以通过创建一个自定义的UIViewController来实现这个效果,该UIViewController包含UINavigationController。新建一个名为“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 - Pang Ho Ming
@PangHoMing 如果您能告诉我有关如何设置UINavigationController的更多信息,我可以提供额外的代码/信息。 - Andreas Ley
我的应用程序设置是... StartPage(启动器页面)----模态转换-->View1----(导航推送)---->View2----(再次推送)--->View3 ........ 我想做的是在view2和view3底部有一个所谓的固定UIView...... 我已经按照您的指示创建了“CustomeViewController Class”,在View2.m的.m中导入,然后分配初始化了一个CustomerViewController *customViewController 然后我就不知道该怎么做了||| - Pang Ho Ming
@PangHoMing 我认为最简单的方法是在AppDelegate中初始化CustomViewController。我很难给出更多建议,因为我不知道您如何创建项目(使用哪些xib文件等)。您能否将代码发送给我? - Andreas Ley
@AndreasLey,你能否为此编写一个小型演示项目?我的问题在于,你正在将导航控制器的视图用作自定义视图控制器视图的子视图。我正在尝试找到一种解决方案来实现类似的效果,但是我无法像这样使用UINavigationController。虽然我同意从理论上讲,你的架构/设置逻辑上是这个人需要解决他的问题的。 - Daniel

0
为什么不将这个静态子视图添加到与您的导航控制器视图相同的级别(通常是接近UIWindow的级别)。只需在那里添加它并使其成为最顶层即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接