zoukankan      html  css  js  c++  java
  • iOS自定义NavigationBar

    日常开发中少不了用到UINavigationController,但是很多情况都要自定义NavigationBar。依稀记得自己刚开始也踩了好多坑,凑今天有空,就把想到的写下来。有时间了,考虑再把自定义TabBar写一下。

    1.修改Navigationbar

    navigationBar其实有三个子视图,leftBarButtonItem,rightBarButtonItem,以及titleView。

    1.1  方法一:alloc一个UINavigationBar ,并给alloc出来的NavigationBar设置button及view,与系统的NavigationBar没有着明显区别。如果在没有NavigationController管理的时候想要添加一个NavigationBar,就这方法还是可以的。。。但是添加到可以滚动的视图(如tableView)的时候,会随着视图的滚动而滚动,不建议使用,代码就不贴了。

    1.2 方法二: 修改系统UINavigationBar的leftBarButtonItem,rightBarButtonItem,以及titleView

    //设置左button
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleDone target:self action:@selector(test)];
        self.navigationItem.leftBarButtonItem = leftItem;
    //设置右button

      UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStyleDone target:self action:@selector(test)];   self.navigationItem.rightBarButtonItem = rightItem;
        //修改titleView字体颜色
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor redcolor] forKey:NSForegroundColorAttributeName];
        self.navigationController.navigationBar.titleTextAttributes = dict;

    navigationBar的半透明效果,可以通过

    self.navigationController.navigationBar.translucent = NO;

    关闭。

    1.3 隐藏navigationBar,自定义View代替navigationBar

    此方法简单粗暴。自定义性强,一个普通的UIView,可以在任何位置放置自己想要的任何控件,设置各种颜色。

    问题在与子视图布局的时候需要注意起始Y值为64(状态栏20 + navigationBar 44)。视图过多的时候是很让人崩溃的一件事。

        self.navigationController.navigationBarHidden = NO;
        self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil]lastObject];
        [self.headerView.menuButton addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
        self.headerView.frame = CGRectMake(0, 20, kScreenW, 44);
        [self.view addSubview:self.headerView];

    1.4  将自定义View添加到titleView上

    不隐藏navigationBar,然后将自定义的View添加到titleView上,好处在于,不用考虑坐标问题。和系统的navigationBar使用起来一样简单,还增加了自定义性。唯一不足是titleView的frame不是整个navigationBar,看起来颜色不统一,解决方法是将navigationBar背景颜色调整为和自定义View一样。

        self.navigationController.navigationBarHidden = NO;
        HeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil]lastObject];
        headerView.frame = CGRectMake(0, 20, kScreenW, 44);
        self.navigationController.navigationBar.translucent = NO;
        self.navigationItem setHidesBackButton = YES;
        //navigationBar颜色与headerView颜色相同
        self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
        self.navigationItem.titleView = headerView;
  • 相关阅读:
    docker工具链概述
    Microsoft.AspNetCore.Authentication.Cookies从入门到精通 (二)
    Microsoft.AspNetCore.Authentication.Cookies从入门到精通 (一)
    阿贝云免费虚拟主机使用体验
    Topshelf 秒建 Windows 服务
    一次兼职项目开发的经历
    修改了my.ini没有效果,MySql的字符集还是没有变成utf8——mysql中文乱码
    【转载】Fiddler工具使用介绍(一)
    C#中$的用法
    系统开发常用模块
  • 原文地址:https://www.cnblogs.com/code-cd/p/4801661.html
Copyright © 2011-2022 走看看