UINavigationBar without UINavigationController

//Creating the plain Navigation Bar

UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.topItem.title = @"title";
[self.view addSubview:headerView];

You can implement the delegate method -positionForBar: of the UINavigationBarDelegate protocol and simply return UIBarPositionTopAttached.

Example:

-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}

//if you're not using a `UINavigationController` and instead
//simply want a `UINavigationBar` then use the following method as well
-(void)testMethod
{
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setDelegate:self];
    [self.view addSubview:navBar];
}

Hope this helps.


You can't modify the status bar background color directly but since it is transparent (since iOS7), you can place a UIView of 20px in height behind the status bar with the desired color and it will appear as if the status bar has a background color.

As for the UINavigationBar, it's just modifying a UINavigationBar object that will help.

Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //...

    //ISSUE 1: StatusBar Background
    UIView *vwStatusBarUnderlay = [[UIView alloc] init];
    [vwStatusBarUnderlay setFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    [vwStatusBarUnderlay setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:vwStatusBarUnderlay];
    //[vwStatusBarUnderlay sendSubviewToBack:self.view];

    //ISSUE 2: NavigationBar
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setTranslucent:NO];

    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    [navItem setTitle:@"Favoriter"];
    [navItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]];

    [navBar setItems:@[navItem]];
    [self.view addSubview:navBar];
}