3

I have created a navigation controller with a stack of view controllers.

I want to add a subview at the bottom which stays static (does not move) while the user navigates between this stack of views.

Like in some apps, there is an "ads bar" at the bottom.

How can I do this?

Mat
  • 202,337
  • 40
  • 393
  • 406
Pang Ho Ming
  • 1,299
  • 10
  • 29

2 Answers2

13

If I understand correctly, this is what you want:

UIView below UINavigationController or below TabBar

You can to this by creating a custom UIViewController that encloses the UINavigationController. Create a new class called "CustomViewController", and paste the following code:

Interface

#import <UIKit/UIKit.h>

@interface CustomViewController : UIViewController

- (id)initWithViewController:(UIViewController*)viewController bottomView:(UIView*)bottomView;

@end

Implementation:

#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

Now to use the CustomViewController:

UIView *myBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
myBottomView.backgroundColor = [UIColor redColor];

CustomViewController *customViewController = [[CustomViewController alloc] initWithViewController:<yourNavigationController> bottomView:myView];
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
  • oh,,, i have problem while implement it, may I get a implemented source code? which will be so so great :D – Pang Ho Ming May 09 '12 at 11:19
  • @PangHoMing If you tell me more about how you are setting up your UINavigationController, I can provide additional code/information. – Andreas Ley May 09 '12 at 11:44
  • My application setting up is ... StartPage(Launcher Page)----modal transition-->View1----(navigation Push)---->View2----(push again)--->View3 ........ what i want to do is to have a so called fixed UIView at the bottom of view2 and view3 ....... i have followed you instruction and created the "CustomeViewController Class" import in the .m of my View2.m ,,, and then alloc init a CustomerViewController *customViewController THEN I CANT FIGURE OUT HOW TO DO NEXT||| – Pang Ho Ming May 09 '12 at 13:19
  • @PangHoMing I think it would be easiest if you initialized the CustomViewController right in the AppDelegate. It's hard to give more advice, because I don't know how you have created your project (where xib files are used etc.). Do you think you could send me your code? – Andreas Ley May 09 '12 at 20:43
  • @AndreasLey can you please put together a small demo project for this. My issue here is that you're using a navigation controller's view as a subview of the custom view controller's view. I am trying to find a solution to make something similar and as far I cannot use a UINavigationController like this. Although I agree that in theory, your architecture/setup is logically what this guys needs to solve his problem. – Daniel Sep 11 '12 at 19:54
0

Why not to add this static subview at the same level as your Navigation Controller view (usually level near UIWindow). Just add it there and make it top most.

Denis
  • 6,313
  • 1
  • 28
  • 27