Failing make Navigation Bar of an UINavigationCont

2019-09-02 09:54发布

问题:

This question already has an answer here:

  • How to make the Navigation Bar of an UINavigationController display at the bottom? 4 answers

i am trying to make Navigation Bar of an UINavigationController display at the bottom.

Put this in the viewWillApprear, it can work. But it go back to the top when come back via dismiss modal view from other views. Even this will be called again, it just stay on the top. So odd !

CGRect navBarOldCGRect = self.navigationController.navigationBar.frame;

CGRect navBarNewCGRect = navBarOldCGRect;
navBarNewCGRect.origin.x = 0;
navBarNewCGRect.origin.y = viewFrame.size.height - navBarOldCGRect.size.height -navBarOldCGRect.origin.y;
navBarNewCGRect.size.width = navBarOldCGRect.size.width;
navBarNewCGRect.size.height = navBarOldCGRect.size.height;

self.navigationController.navigationBar.frame = navBarNewCGRect;


NSLog(@"viewFrame : %@", CGRectCreateDictionaryRepresentation(viewFrame));
NSLog(@"navBarOldCGRect : %@", CGRectCreateDictionaryRepresentation(navBarOldCGRect));
NSLog(@"navBarNewCGRect : %@", CGRectCreateDictionaryRepresentation(navBarNewCGRect));
NSLog(@"result : %@", CGRectCreateDictionaryRepresentation(self.navigationController.navigationBar.frame));

Output of log like this;

viewFrame : {
    Height = 1024;
    Width = 768;
    X = 0;
    Y = 0;
}
navBarOldCGRect : {
    Height = 44;
    Width = 768;
    X = 0;
    Y = 20;
}
navBarNewCGRect : {
    Height = 44;
    Width = 768;
    X = 0;
    Y = 960;
}
result : {
    Height = 44;
    Width = 768;
    X = 0;
    Y = 960;
}

But not change on screen. Navbar still is still at the top of screen.

What is the point i am missing?

回答1:

This is enough to create navigation bar. Put it on viewDidLoad or viewWillAppear [self.navigationController.navigationBar setHidden:NO];

//New Answer

-(void) tabBarFunction
{
homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
recentSearchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
myFavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
myOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];


    bgImageView.frame = CGRectMake(0, 0,320, 480);
    [bgImageView setImage:[UIImage imageNamed:@"bg.png"]];
    [homeBtn setFrame:CGRectMake(0, 388, 80, 48)];
    [recentSearchBtn setFrame:CGRectMake(80, 388, 80, 48)];
    [myFavBtn setFrame:CGRectMake(160, 388, 80, 48)];
    [myOrderBtn setFrame:CGRectMake(240, 388, 80, 48)];




[homeBtn setImage:[UIImage imageNamed:@"home_sel_btn.png"] forState:UIControlStateNormal];
[recentSearchBtn setImage:[UIImage imageNamed:@"search_unsel_btn.png"] forState:UIControlStateNormal];
[myFavBtn setImage:[UIImage imageNamed:@"fav_unsel_btn.png"] forState:UIControlStateNormal];
[myOrderBtn setImage:[UIImage imageNamed:@"order_unsel_btn.png"] forState:UIControlStateNormal];

[homeBtn addTarget:self action:@selector(homeBtnTapped) forControlEvents:UIControlEventTouchUpInside];
[recentSearchBtn addTarget:self action:@selector(recentSearchTapped) forControlEvents:UIControlEventTouchUpInside];
[myFavBtn addTarget:self action:@selector(myFaveBtnTapped) forControlEvents:UIControlEventTouchUpInside];
[myOrderBtn addTarget:self action:@selector(myOrderBtnTapped) forControlEvents:UIControlEventTouchUpInside];


[self.view addSubview:homeBtn];
[self.view addSubview:recentSearchBtn];
[self.view addSubview:myFavBtn];
[self.view addSubview:myOrderBtn];

}


回答2:

Try this

bottomNav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0,  self.view.frame.size.height - 20, self.view.frame.size.width, 44.0)];  
bottomNav.barStyle = UIBarStyleBlackOpaque;  
[self.parentViewController.view addSubview:bottomNav]; 

You can try to make like this

    NSArray *items = [NSArray arrayWithObjects:
                      ..
                      nil];

    self.navigationController.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
    self.navigationController.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
    self.toolbarItems = items;