Failing make Navigation Bar of an UINavigationController display at the bottom programmatically [duplicate]

瘦欲@ 提交于 2019-12-13 05:30:59

问题


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;


来源:https://stackoverflow.com/questions/20399313/failing-make-navigation-bar-of-an-uinavigationcontroller-display-at-the-bottom-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!