【问题标题】:How to customize UINavigationBar如何自定义 UINavigationBar
【发布时间】:2015-04-17 02:55:08
【问题描述】:

我在自定义 UINavigationBar 中苦苦挣扎。我想自定义的原因是因为在不同的视图控制器中可能会发生相同的操作。我发布了一些图片作为示例。

从上面的图片可以看出。我想你明白我的意思。我一直在尝试和寻找,但我没有得到我想要的答案。此外,我尝试使用 UIView 来完成此操作,它有效,但我认为这不是正确的方法。因此,我需要你们的帮助来引导我走上正确的道路。 :) :) 谢谢。

【问题讨论】:

    标签: ios xcode uinavigationbar uinavigationitem


    【解决方案1】:

    您可以像这样为导航栏设置自定义标题视图。

    //To hide default back button
    self.navigationItem.hidesBackButton=YES;
    
    //Title View for Navigation
    UIView *navTitle1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [navTitle1 setBackgroundColor:[UIColor lightGrayColor]];
    
    //Left View button and separator
    UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [backBarButton setTitle:@"Back" forState:UIControlStateNormal];
    
    UIView *ttlview1 = [[UIView alloc] initWithFrame:CGRectMake(51, 0, 1, 44)];
    [ttlview1 setBackgroundColor:[UIColor darkGrayColor]];
    
    
    //Center view with two buttons and seprator for them
    UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 121, 44)];
    [middleView setBackgroundColor:[UIColor clearColor]];
    [middleView setCenter:navTitle1.center];
    
    UIButton *postBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    [postBarButton setTitle:@"Post" forState:UIControlStateNormal];
    
    UIView *ttlview2 = [[UIView alloc] initWithFrame:CGRectMake(middleView.frame.size.width/2, 0, 1, 44)];
    [ttlview2 setBackgroundColor:[UIColor darkGrayColor]];
    
    UIButton *memeBarButton = [[UIButton alloc] initWithFrame:CGRectMake((middleView.frame.size.width/2)+1, 0, 60, 44)];
    [memeBarButton setTitle:@"Meme" forState:UIControlStateNormal];
    
    [middleView addSubview:ttlview2];
    [middleView addSubview:postBarButton];
    [middleView addSubview:memeBarButton];
    
    //Right button with seprator before that
    UIView *ttlview3 = [[UIView alloc]     initWithFrame:CGRectMake(self.view.frame.size.width-71, 0, 1, 44)];
    [ttlview3 setBackgroundColor:[UIColor darkGrayColor]];
    

    您可以参考THIS了解更多详情。

    【讨论】:

    • 这是我之前做的。这是正确的做法而不是自定义。
    • 我认为,这就是方式,您可以进行高级别的自定义,默认导航栏不提供直接通过本机功能和按钮进行这种自定义。
    【解决方案2】:

    您可以使用此文档从 2 个视图创建 2 个自定义 UIBarButtonItem:

    来自 Apple 文档: 使用指定的自定义视图初始化新项目。

    - (id)initWithCustomView:(UIView *)customView
    

    参数 customView:代表项目的自定义视图。 返回值 具有指定属性的新初始化项。

    • 左侧栏项目:1 个视图包含 3 个按钮:Project、Kiara Paradize、Artist Impression。

    • 右栏项目:1 个视图包含 4 个按钮:注册 |登录(用户登录时的个人资料按钮)|汉堡菜单

    然后:

    • self.navigationItem.leftBarButtonItem = UIBarButtonItem 从左边 自定义视图
    • self.navigationItem.rightBarButtonItem = 来自右侧自定义视图的 UIBarButtonItem

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2010-10-16
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多