DevManny
DevManny

Reputation: 1326

How to add a custom UIView to Navigation Bar in iOS7+ like this

Is there a way to insert any UIView in NavigationBar like the image below?

enter image description here

Upvotes: 5

Views: 6036

Answers (3)

Cloud Music
Cloud Music

Reputation: 81

An alternative solution is to use a regular UIView as a NavBar, and by saying that what I mean is you have to set the UIView in navbar place and hide the NavigationController bar in the main VC and show it in the next one read this article Here https://medium.com/me/stats/post/54bc5fc56d6c

Upvotes: 0

John Wallace
John Wallace

Reputation: 161

In the code below, I embed a view into the navbar, where my embedded view draws the background and the new buttons. The code also deals with relaying out the view when the device is rotated.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    if (view.superview == nil) {
        [self.navigationBar addSubview:view];
        [self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }

    CGRect frame = self.navigationBar.frame;
    frame.size.height = MY_NAVBAR_HEIGHT;
    self.navigationBar.frame = frame;
    view.frame = self.navigationBar.bounds;
}

Upvotes: 1

saurabh
saurabh

Reputation: 6775

Is this not working?

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];

[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"Controls"];
[navBar pushNavigationItem:navItem animated:NO];

Upvotes: 1

Related Questions