失败使UINavigationController的导航栏以编程方式显示在底部

时间:2013-12-05 11:52:55

标签: ios objective-c uinavigationbar cgrect

我正在尝试在底部制作UINavigationController显示的导航栏。

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));

像这样输出日志;

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;
}

但不要在屏幕上改变。 Navbar仍然位于屏幕顶部。

我错过了什么?

2 个答案:

答案 0 :(得分:-1)

试试这个

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]; 

您可以尝试这样做

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

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

答案 1 :(得分:-1)

这足以创建导航栏。把它放在viewDidLoad或viewWillAppear [self.navigationController.navigationBar setHidden:NO];

//新答案

-(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];

}