UINavigationBar && UITabBar

2016-09-10  本文已影响0人  BrightFuture

UINavigationBar

UINavigationBar
// 导航栏的内容由栈顶控制器的navigationItem属性决定
// 左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;
// 中间的标题视图
@property(nonatomic,retain) UIView *titleView;
// 中间的标题文字
@property(nonatomic,copy) NSString *title;
// 左上角的视图
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;UIBarButtonItem *rightBarButtonItem 
// 右上角的视图
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
  - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count > 0) {
      /* 这里之所以要判断childViewControllers的count,是因为navigationController初始化的时候设置rootViewController时系统会自动调用push方法,
         但是rootController的leftBarButtonItem我们是不需要设为返回按钮的,
         所以要过滤掉,当super的push方法调用前,childViewControllers是不会加入viewController的,
         所以rootViewController经过此方法时childViewControllers.count等于0 */
      // 隐藏将要跳转到的controller的UITabBar
      viewController.hidesBottomBarWhenPushed = YES;
      UIButton *backBtn = [UIButton   buttonWithType:UIButtonTypeCustom];
      [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
      [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
      [backBtn setTitle:@"返回" forState:UIControlStateNormal];
      [backBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
      [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
      // 设置button的尺寸与内容一致
      [backBtn sizeToFit];
      [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
      // 设置整个button内容左对齐
       backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
      viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
      // 之所以设置leftBarButtonItem而不用backBarButtonItem,是因为backBarButtonItem通过initWithCustomView:初始化不起作用
    }
    // 放在最后面的好处是,super的push方法调用时,也会调用将要跳转到的controller的viewDidLoad方法,如果有些需要跳转的controller需要自己的leftBarButtonItem时,就会替换掉之前自定义的返回按钮
    [super pushViewController:viewController animated:animated];
}

6.通过apperance设置统一样式,要想通过此方式设置全局属性需要满足两个条件:

 + (void)initialize {
   // 设置navigationBar的统一背景
   UINavigationBar *navigationBar = [UINavigationBar appearance];
   [navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar_background"] forBarMetrics:UIBarMetricsDefault];
  }

UITabBar

UITabBar
UITabBarItem *item = [UITabBarItem appearance];
// 设置默认字体样式
NSMutableDictionary *attr = [[NSMutableDictionary alloc]init]; 
attr[NSFontAttributeName] = [UIFont systemFontOfSize:12]; 
attr[NSForegroundColorAttributeName] = [UIColor grayColor]; 
[item setTitleTextAttributes:attr forState:UIControlStateNormal];
// 设置选中字体样式,注意不要用上面的字典了,不同state下的属性要用不同的字典来装
NSMutableDictionary *attrSelected = [[NSMutableDictionary alloc]init]; 
attrSelected[NSFontAttributeName] = attr[NSFontAttributeName]; 
attrSelected[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
 [item setTitleTextAttributes:attrSelected forState:UIControlStateSelected];
[self setValue:tabBar forKeyPath:@"tabBar"];
上一篇 下一篇

猜你喜欢

热点阅读