美文网首页
有关Nav和Tab属性和方法的一些细节记录

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

作者: ethan_cun | 来源:发表于2016-12-13 17:14 被阅读45次

状态栏的设置

情形:设置欢迎页影藏,首页显示:
在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];

相关文章

网友评论

      本文标题:有关Nav和Tab属性和方法的一些细节记录

      本文链接:https://www.haomeiwen.com/subject/iydbmttx.html