Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Trying to add UINavigation Bar programmatically but it is not showing up

I have done lazy-loading of navBar and also set a frame as follows :

- (UINavigationBar *) navBar {
    if (_navBar) {
        _navBar = [[UINavigationBar alloc] init];
        [_navBar setBackgroundColor:[UIColor blueColor]];
        _navBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    }
    return _navBar;
}

My ViewController code looks as follows but the navigationBar is not showing up.

//
//  MySecondViewController.m
//  Example2

#import "MySecondViewController.h"

@interface MySecondViewController ()
@property (nonatomic) UIButton *stocksButton;
@property (nonatomic) UIButton *weatherButton;
@property (nonatomic) UINavigationBar *navBar;

@end

@implementation MySecondViewController

- (id) init {

    if (self = [super init]) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    UIColor *tealColor = [UIColor colorWithRed:200.0/255.0 green:191.0/255.0 blue:231.0/255.0 alpha:1.0];
    self.view.backgroundColor = tealColor;




    [self.view addSubview:self.stocksButton];
    [self.view addSubview:self.weatherButton];
    [self.view addSubview:self.navBar];

    NSDictionary *elementsDict = NSDictionaryOfVariableBindings(_stocksButton, _weatherButton);



    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=150)-[_stocksButton]-(<=20)-[_weatherButton(==_stocksButton)]-(>=150)-|"
                                                                     options:NSLayoutFormatDirectionLeadingToTrailing
                                                                     metrics:nil
                                                                       views:elementsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(<=50)-[_stocksButton]-(<=50)-|"
                                                                      options:NSLayoutFormatDirectionLeadingToTrailing
                                                                      metrics:nil
                                                                        views:elementsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(<=50)-[_weatherButton]-(<=50)-|"
                                                                      options:NSLayoutFormatDirectionLeadingToTrailing
                                                                      metrics:nil
                                                                        views:elementsDict]];



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI elements

- (UIButton *) stocksButton {
    if (!_stocksButton) {
        _stocksButton = [[UIButton alloc] init];
        [_stocksButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        UIColor *goldenrodColor = [UIColor colorWithRed:218.0/255.0 green:165.0/255.0 blue:32.0/255.0 alpha:1.0];
        [_stocksButton setBackgroundColor:goldenrodColor];
        [_stocksButton setTitle:@"Stocks" forState:UIControlStateNormal];
    }
    return _stocksButton;
}

- (UIButton *) weatherButton {
    if (!_weatherButton) {
        _weatherButton = [[UIButton alloc] init];
        [_weatherButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        UIColor *rawsiennaColor = [UIColor colorWithRed:199.0/255.0 green:97.0/255.0 blue:20.0/255.0 alpha:1.0];
        [_weatherButton setBackgroundColor:rawsiennaColor];
        [_weatherButton setTitle:@"Weather" forState:UIControlStateNormal];
        CGRect frame = _weatherButton.frame;
        frame.size = _weatherButton.intrinsicContentSize;

    }
    return _weatherButton;
}

- (UINavigationBar *) navBar {
    if (_navBar) {
        _navBar = [[UINavigationBar alloc] init];
        [_navBar setBackgroundColor:[UIColor blueColor]];
        _navBar.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height);
    }
    return _navBar;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

1 Answer

You're setting the frame of the nav bar but not the autoresize mask. Try adding:

_navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;