iOS——UINavigationBar

来源:互联网 发布:无线云台网络摄像机 编辑:程序博客网 时间:2024/06/08 23:42

一、UINavigationItem

1. 概述

1)UINavigationItem 是用于显示在 UINavigationBar 上面的一个视图控件,可以显示标题、视图等,直接继承于 UIView
2)每个 UIViewController 对象都可以通过 navigationItem 属性获取当前 UIViewController 的 UINavigationItem 对象

2. 设置标题

1)文字标题

@property(nullable,nonatomic,copyNSString * 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. 设置按钮

每一个 UINavigationItem 都可以设置一个(或多个)左侧的按钮,一个(或多个)右侧的按钮,先来看设置的那个的左侧(右侧)的按钮,了解了单个的按钮,多个的按钮也就很方便了
注意 : 在 UINavigationItem 中的按钮并不是 UIButton,而是 UIBarButtonItem

设置单个左侧(右侧)按钮

@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);



1)设置单个左侧按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStylePlain target:nil action:nil];
注意 : 在创建 UIBarButtonItem 对象时必须指定 目标-动作对,此处为了方便,不设置 目标-动作对


2)设置多个左侧按钮

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.设置返回按钮

在 UINavigationController 对象中的 UIViewController 对象中,如果进行了压栈操作,那么会显示栈顶的 UIViewController 对象,此时,新的 UIViewController 会在 UINavigationBar 显示一个返回按钮,用于返回次栈顶的 UIViewController 对象;如果次栈顶的 UIViewController 没有设置 UINavigationItem 的 title 属性,则显示 “back”;如果设置了 title 属性,则显示 title 的名称

如果次栈顶的 UIViewController 对象设置了 title 为 “First”,则栈顶的 UIViewController 应该如下

否则就应该如下


通过此属性就可以自动以返回按钮

@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];
此时,效果如下



在这里我们讨论下关于 backBarButtonItem 的优先级问题 :
a. 如果在 second 中定义了 leftBarButtonItem(leftBarButtonItems),那么返回按钮就是 leftBarButtonItem(leftBarButtonItems)
b. 如果在 second 中没有设置 leftBarButtonItem,但是在 first 中设置了 backBarButtonItem,那么返回按钮就是 first 中设置的 backBarButtonItem
c. 如果上述两种情况都没有,那么系统会自动使用 first 的 title 属性名作为标题,如果没有设置 title,那么就用 “back”
总结 : 一般来说,我们更愿意用 leftBarButtonItem 代替 backBarButtonItem,因为使用 leftBarButtonItem 我们可以在 出栈前完成一些自定义的操作,但是使用 backBarButtonItem 则不容易完成

也可将返回按钮隐藏,通过下面这个属性

@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. 概述
如图

UINavigationController 会在其上方显示一个 UINavigationBar 对象,该对象默认会显示,可通过 UINavigationController 的 navigationBar 属性获得

1. 设置风格

@property(nonatomic,assign)UIBarStyle barStyleUI_APPEARANCE_SELECTOR__TVOS_PROHIBITED;

UIBarStyle 枚举如下
typedef NS_ENUM(NSInteger, UIBarStyle) {    UIBarStyleDefault          = 0,  // 默认风格,透明    UIBarStyleBlack            = 1,  // 透明黑色    UIBarStyleBlackOpaque      = 1,  // 不透明黑色,被废弃,可以使用 UIBarStyleBlack 和 translucent 设置为 NO 代替    UIBarStyleBlackTranslucent = 2,  // 透明黑色,被废弃,可以使用 UIBarStyleBlack};

设置是否透明,默认为 YES;在 iOS 6 之前默认为 NO,

@property(nonatomic,assign,getter=isTranslucent)BOOL translucent NS_AVAILABLE_IOS(3_0);



使用 UIBarStyleDefault 风格就如上面的图片所示

使用 UIBarStyleBlack 风格如下,此时是黑色透明
self.navigationBar.barStyle = UIBarStyleBlack;



使用 UIBarStyleBlackOpaque,此时是黑色不透明
self.navigationBar.barStyle = UIBarStyleBlack;self.navigationBar.translucent = NO;


2. 设置颜色

设置颜色可通过两个属性来说设置
1)设置背景颜色

@property(nullable,nonatomic,strong)UIColor *barTintColor


2)设置 UIBarButtonItem 的颜色

@property(null_resettable,nonatomic,strong)UIColor *tintColor;


// 设置 bar 的背景颜色self.navigationBar.barTintColor = [UIColor blueColor];    // 设置 bar 内的 按钮 颜色self.navigationBar.tintColor = [UIColor redColor];
效果如下



2. UINavigationBar 内可以保存一组 UINavigationItem 对象

注意 : 在 UINavigationController 的 navigationItem 属性中,有这样的一句话
@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 使用


1)UINavigationBar 保存 UINavigationItem 对象的数组,默认为 nil

@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];
效果如下



0 0
原创粉丝点击