有关Nav和Tab属性和方法的一些细节记录

状态栏的设置

情形:设置欢迎页影藏,首页显示:
在info.plist里增加 Status bar is initially hidden,设置为yes
然后在首页或者父类控制器里设置

    // MARK: - 状态栏
    override var prefersStatusBarHidden: Bool{
        return false
    }

这里需要注意第二个方法:
1、如果ViewController不是UINavigationController的子类,调用 preferredStatusBarStyle 是可以改变状态栏文字的颜色,相反则不能;

因为 UINavigationController 有自己的状态栏,需要自己管理所以它的子类是不会走 preferredStatusBarStyle 方法;如果想要某个VC 改变,可以使用 UINavigationBar.barStyle属性

self.navigationController?.navigationBar.barStyle = .black

或者 //隐藏导航栏后 系统会调用 preferredStatusBarStyle 方法

self.navigationController.navigationBarHidden=YES;

标题

  //tab和nav的title会全部更改:
    self.title = @"首页";
   //nav会更改 tab不会更改:
    self.navigationItem.title = @"首页";

导航栏颜色

    //修改导航栏的颜色
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];

导航栏下个界面返回按钮

    //修改导航栏下个界面返回按钮的文字 注意 修改的是下一个界面的返回按钮:
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];

导航栏返回按钮图片

// 修改导航栏返回按钮的图片 返回按钮图片大小为42*42 这里不能直接按照下面的方式设置,不然当界面上有弹框UIAlertController时,图片会向下偏移:
        let leftBBi = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(personalSettingBack))
        leftBBi.image = UIImage(named: "nav_back")
        leftBBi.tintColor = HHGK_WHITE_COLOR
        self.navigationItem.leftBarButtonItem = leftBBi

// 正确的做法应该是自定义一个按钮, 然后设置为leftBarButtonItem:
        let backBtn = UIButton(type: .custom)
        backBtn.setImage(UIImage.init(named: "nav_back"), for: .normal)
        backBtn.tintColor = HHGK_WHITE_COLOR
        backBtn.frame = CGRect(x: 0, y: 0, width: 42, height: 42)
        backBtn.addTarget(self, action: #selector(personalSettingBack), for: .touchUpInside)
        
        let backBBi = UIBarButtonItem.init(customView: backBtn)
        
        self.navigationItem.leftBarButtonItem = backBBi

导航栏文字样式

    //修改导航栏中间文字的样式:
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
swift:
    self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]

工具栏的颜色:

 //修改工具栏的颜色:
    self.tabBarController.tabBar.barTintColor = [UIColor grayColor];
//修改工具栏下面文字的颜色:
    self.tabBarController.tabBar.tintColor = [UIColor blackColor];  

透明

//把顶部这个navigationbar设置为透明呢,能够让下面的图片显示出来,但是返回按钮不透明:
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

线条

 //去掉导航栏下面的线条:
    self.navigationController.navigationBar.shadowImage = [UIImage new];

未选中与选中时的图片

//设置UITabBarItem未选中与选中时的图片:
   [_hotTabItem setFinishedSelectedImage:[UIImage imageNamed:@"1_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"1"]];

badgeValue

//设置UITabBarItem的badgeValue:
  //指定界面
   [self.tabBarController.tabBar.items objectAtIndex:2].badgeValue = [NSString stringWithFormat:@"%ld", ++ goodsCount];
  //当前界面
        _mine.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", count];
  //为0时清除
      _mine.navigationController.tabBarItem.badgeValue = nil;

tabBar样式

//设置tabBar样式

[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[selfmyThemeColor]}forState:UIControlStateSelected];

[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorredColor]}forState:UIControlStateNormal];

//选中的颜色(图片+图片下面文字的颜色,iOS8.0以后被废弃了)

self.tabBarController.tabBar.selectedImageTintColor= [selfmyThemeColor];

//未选中的颜色(图片+图片下面文字的颜色,没被废弃)

self.tabBarController.tabBar.unselectedItemTintColor= [UIColorgreenColor];

dismiss回到目标控制器

// dismiss方式回到目标控制器:
/*
(1) presentedViewController:The view controller that is presented by this view controlller(read-only),被本视图控制器present出来的的视图控制器(只读)
(2) presentingViewController:The view controller that presented this view controller. (read-only),present出来本视图控制器的视图控制器(只读)
*/
        UIViewController *vc = self.presentingViewController;
        while (![vc isKindOfClass:[TFTravelllerLoginViewController class]]) {
        
            vc = vc.presentingViewController;
        }
        
        [vc dismissViewControllerAnimated:YES completion:nil];

push到下个界面隐藏tabBar pop回去显示tabBar

// 注意是设置目标控制器的hidesBottomBarWhenPushed属性
EaseMessageViewController *messageVc = [[EaseMessageViewController alloc] initWithConversationChatter:contactName conversationType:EMConversationTypeChat];
messageVc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:messageVc animated:YES];

pop回到目标控制器

//pop方式回到目标控制器:
        for (UIViewController *disVc in self.navigationController.viewControllers) {
            
            if ([disVc isKindOfClass:[TFTravelllerLoginViewController class]]) {
                [self.navigationController popToViewController:disVc animated:YES];
            }
        }

UISearchBar自定义

    searchBar.backgroundImage = [CzyTools imageWithColor:[UIColor colorWithHexString:@"#F5F5F5"]];
    searchBar.backgroundColor = [UIColor clearColor];
    [searchBar setSearchFieldBackgroundImage:[CzyTools imageWithColor:[UIColor colorWithHexString:@"#F5F5F5"]] forState:UIControlStateNormal];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 相逢不饮空杯去,洞口桃花也笑人。 朋友难得,好朋友更难得,几十年的好朋友更是难得,所以应当好好珍惜面前的朋友,所有...
    九小飞阅读 695评论 4 0
  • 参考书目 《用图秀演讲》 一个精彩的故事可以为演讲增色。如何讲好故事呢?书中给我们分享了一个讲故事的好办法,即“美...
    萌小Q在路上阅读 297评论 8 9
  • 今年暑假小叔从浙江回来看望爷爷奶奶,去年因为手头上还有事情,所以就没有回家。不经意间就是两个...
    晨曦朦胧岁月静好阅读 347评论 1 3
  • 我上辈子大概是个车夫。 小时候一旦汽车飞驰而过,定会猛嗅一口浓烈的汽油味,叫一声“好香”。有次到镇上的...
    维扬客阅读 190评论 0 2