免费开源的iOS开发学习平台

UINavigationController介绍:16-toolbar(工具栏)

导航控制器中,除了在顶部具有一个navigationBar之外,在底部还有一个toolBar。在默认情况下,toolBar是被隐藏起来的。toolBar的使用方法和navigationBar完全一致,在toolBar上放置的按钮也是UIBarButtonItem。

toolBar简介

toolBar可以理解成为显示在屏幕底部的navigationBar,它具有同NavigationBar一样的特性,上面的按钮都是UIBarButtonItem类的对象。toolBar常出现在新闻类App中,或者WKWebView中,用于前进后退等操作。如下图所示:

toolbar属于UIToolbar类,在UIToolbar类中,常用的属性和方法有:

  • barStyle: toolBar的样式,默认情况下如上图所示的蓝色主色样式
@property(nonatomic) UIBarStyle barStyle;
  • items: 存放toolBar上的UIBarButtonItem类按钮对象
@property(nullable,nonatomic,copy) NSArray *items;
  • barTintColor:toolBar背景颜色
@property(nullable, nonatomic,strong) UIColor *barTintColor;
  • tintColor:toolBar控件渲染的颜色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
  • 设置toolBar的背景图片
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;

导航控制器中的toolBar

在导航控制器(UINavigationController)中,有如下属性与toolbar相关。

  • toolBar对象,只读属性,因此我们不能修改该属性的值
@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar;
  • toolBar的隐藏与显示,默认情况下是隐藏的
@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden;
  • 导航控制器使用自定义toolbar。
- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;

视图控制器中的toolbarItems

在视图控制器UIViewController对象中,有一个toolbarItems属性,用于存放toolbar上面的所有按钮。与navigationItem不同的是,其不再区分左边按钮/右边按钮等。

@interface UIViewController (UINavigationControllerContextualToolbarItems)
@property (nullable, nonatomic, strong) NSArray *toolbarItems;
@end