【问题标题】:Failing make Navigation Bar of an UINavigationController display at the bottom programmatically [duplicate]未能以编程方式使 UINavigationController 的导航栏显示在底部[重复]
【发布时间】:2013-12-05 11:52:55
【问题描述】:

我正在尝试使 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;
}

但不会在屏幕上改变。导航栏仍位于屏幕顶部。

我错过了什么?

【问题讨论】:

    标签: ios objective-c uinavigationbar cgrect


    【解决方案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];
    
    }
    

    【讨论】:

    • 我不明白。你是在告诉我,隐藏当前导航栏并创建一个新导航栏吗?
    • 导航栏始终可用。我们的工作就是不要隐藏它。
    • 但是导航已经在那里了。在屏幕顶部。我想在屏幕底部制作。
    • 那么你就可以使用这个代码了。在答案中查看我的代码。
    • 我看不出我的问题与您的代码之间有任何关系。我已经知道以编程方式添加按钮了!
    【解决方案2】:

    试试这个

    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;
    

    【讨论】:

    猜你喜欢
    • 2011-08-17
    • 2016-02-12
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多