一、UINavigationItem
1. 概述
2. 设置标题
@property(nullable,nonatomic,copy) NSString * title;
self.navigationItem.title = @"First";
2)视图标题
@property(nullable,nonatomic,strong)UIView * titleView;
UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
titleView.backgroundColor = [UIColor redColor];
self.navigationItem.titleView = titleView;
3)显示副标题,会在 title 正方上 30 显示
@property(nullable,nonatomic,copy) NSString * prompt__TVOS_PROHIBITED;
self.navigationItem.title = @"First";
self.navigationItem.prompt = @"副标题";
3. 设置按钮
@property(nullable,nonatomic,strong)UIBarButtonItem *leftBarButtonItem;
@property(nullable,nonatomic,strong)UIBarButtonItem *rightBarButtonItem;
- (void)setLeftBarButtonItem:(nullableUIBarButtonItem *)item animated:(BOOL)animated;
- (void)setRightBarButtonItem:(nullableUIBarButtonItem *)item animated:(BOOL)animated;
设置多个左侧(右侧)按钮
@property(nullable,nonatomic,copy)NSArray<UIBarButtonItem *> *leftBarButtonItemsNS_AVAILABLE_IOS(5_0);
@property(nullable,nonatomic,copy)NSArray<UIBarButtonItem *> *rightBarButtonItemsNS_AVAILABLE_IOS(5_0);
- (void)setLeftBarButtonItems:(nullableNSArray<UIBarButtonItem *> *)items animated:(BOOL)animatedNS_AVAILABLE_IOS(5_0);
- (void)setRightBarButtonItems:(nullableNSArray<UIBarButtonItem *> *)items animated:(BOOL)animatedNS_AVAILABLE_IOS(5_0);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStylePlain target:nil action:nil];
注意 : 在创建 UIBarButtonItem 对象时必须指定 目标-动作对,此处为了方便,不设置 目标-动作对
UIBarButtonItem * first = [[UIBarButtonItem alloc] initWithTitle:@"First" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem * second = [[UIBarButtonItem alloc] initWithTitle:@"Second" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftBarButtonItems = @[first, second];
4.设置返回按钮
@property(nullable,nonatomic,strong)UIBarButtonItem *backBarButtonItem;
注意 : 如果要自定义返回按钮,则必须在 当前 UIViewController 的上一个 UIViewController 里设置;举例来说,现在 UINavigationController 里有两个 UIViewController 对象,分别命名为 first 和 second,如果我想在 second 的视图里显示的返回按钮是自定义的(比如自定义为 “返回”),则必须在 first 里面写下面这句话
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回2" style:UIBarButtonItemStylePlain target:nil action:nil];
此时,效果如下
@property(nonatomic,assign)BOOL hidesBackButton;
- (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated;
如果设置为 YES,则当前显示的 UIViewController 对象则不会显示返回按钮
还有一个属性,指定为 YES 则可以同时显示 leftBarButtonItem 和 backBarButtonItem,并且 leftBarButtonItem 显示在 backBarButtonItem 的右边;默认为 NO
@property(nonatomic)BOOL leftItemsSupplementBackButton;
在 second UIViewController 中添加如下代码
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftItemsSupplementBackButton = YES;
二、UINavigationBar
1. 设置风格
@property(nonatomic,assign)UIBarStyle barStyleUI_APPEARANCE_SELECTOR__TVOS_PROHIBITED;
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0, // 默认风格,透明
UIBarStyleBlack = 1, // 透明黑色
UIBarStyleBlackOpaque = 1, // 不透明黑色,被废弃,可以使用 UIBarStyleBlack 和 translucent 设置为 NO 代替
UIBarStyleBlackTranslucent = 2, // 透明黑色,被废弃,可以使用 UIBarStyleBlack
};
@property(nonatomic,assign,getter=isTranslucent)BOOL translucent NS_AVAILABLE_IOS(3_0);
self.navigationBar.barStyle = UIBarStyleBlack;
self.navigationBar.barStyle = UIBarStyleBlack;
self.navigationBar.translucent = NO;
2. 设置颜色
@property(nullable,nonatomic,strong)UIColor *barTintColor
@property(null_resettable,nonatomic,strong)UIColor *tintColor;
// 设置 bar 的背景颜色
self.navigationBar.barTintColor = [UIColor blueColor];
// 设置 bar 内的 按钮 颜色
self.navigationBar.tintColor = [UIColor redColor];
效果如下
2. UINavigationBar 内可以保存一组 UINavigationItem 对象
@property(nonatomic,readonly) UINavigationBar *navigationBar; // The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.
就是说,此时的 UINavigationBar 是被 UINavigationController 管理的,所以一系列向 UINavigationBar 的操作(push,pop,setter)都是无效的,并且会报下面的错;这些方法只能在一个单独的 UINavigationBar 中使用,而不是在 UINavigationController 中的 UINavigationBar 使用
@property(nullable,nonatomic,copy)NSArray<UINavigationItem *> *items;
- (void)setItems:(nullableNSArray<UINavigationItem *> *)items animated:(BOOL)animated;
2)入栈或出栈 UINavigationItem 对象
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
- (nullableUINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
3)栈顶 和 次栈顶 的 UINavigationItem 对象
@property(nullable,nonatomic,readonly,strong)UINavigationItem *topItem;
@property(nullable,nonatomic,readonly,strong)UINavigationItem *backItem;
三、UIToolbar
1. UIToolbar 是位于 UINavigationController 下方的一个工具栏,默认是不显示,先要显示它,需要设置属性
@property(nonatomic,getter=isToolbarHidden)BOOL toolbarHidden;
- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated;
self.navigationController.toolbarHidden = NO;
[self.navigationController setToolbarHidden:NO animated:YES];
最下面的就是 UIToolbar,当设置动画效果的时候,它会从右侧滑入
2. 设置风格;同 UINavigationItem 一样,不再赘述
@property(nonatomic)UIBarStyle barStyle;
3. 设置是否透明;默认是 YES,在 iOS 6 之前默认为 NO
@property(nonatomic,assign,getter=isTranslucent)BOOL translucent
4. 设置 UIToolbar 中的 UIBarButtonItem 对象时,必须用到 UIViewController 的分类
UINavigationControllerContextualToolbarItems 中的方法
@interface UIViewController (UINavigationControllerContextualToolbarItems)
@property (nullable, nonatomic, strong) NSArray<__kindof UIBarButtonItem *> *toolbarItems;
- (void)setToolbarItems:(nullable NSArray<UIBarButtonItem *> *)toolbarItems animated:(BOOL)animated;
@end
UIBarButtonItem * add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
[self setToolbarItems:@[add] animated:YES];
效果如下
5. 设置颜色
1)设置 UIToolbar 中的 UIBarButtonItem 的颜色
@property(null_resettable,nonatomic,strong)UIColor *tintColor;
2)设置 UIToolbar 的背景颜色
@property(nullable,nonatomic,strong)UIColor *barTintColor
self.navigationController.toolbar.barTintColor = [UIColor redColor];
self.navigationController.toolbar.tintColor = [UIColor greenColor];
效果如下