Question

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?

Was it helpful?

Solution

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];

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top