iOS_UI开发中容易被忽略的细节之--UINavigationBar.h

前言

navigationBar 作为 UINavigationController 的只读属性,可定制改变的空间很小。

目录

    1. UINavigationBar 的属性和方法介绍
    1. UINavigationBarDelegate

一、UINavigationBar 的属性和方法介绍

// 设置导航条风格,包括状态栏字体、title 字体颜色都跟着改变
// 默认时白底黑字,如果 translucent 为 YES 时,底不是纯白色会有透明效果
// Black 时黑底白字,如果 translucent 为 YES 时,底不是纯黑色会有透明效果
@property(nonatomic,assign) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 定义在 UIInterface.h 里面的枚举
// 默认和 black,另外两个被 Deprecated 
typedef NS_ENUM(NSInteger, UIBarStyle) {
    UIBarStyleDefault          = 0,
    UIBarStyleBlack            = 1,
    
    UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack
    UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
} __TVOS_PROHIBITED;
black_ translucent_NO.png

black_ translucent_YES.png

default_ translucent_NO.png

default_ translucent_YES.png
// 代理对象
@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;
// iOS 7 后新定义了该属性
// 默认是 YES。在 iOS 6 以及之前,默认是 NO
// 如果 barStyle 设置为 UIBarStyleBlackTranslucent,则该属性总是为 YES
// 可以设置为 NO,取消导航条的透明效果,这里有一点要注意,透明与不透明的页面顶部布局不同
// 该属性会对设置的背景图片的图片本身的透明度产生影响
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;
// push 一个自定义的 navigationItem 显示在 navigationBar 的中心
// 如果已经有一个 navigationItem 了,则会在左边显示一个返回按钮
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
// pop 操作,和上面的 push 对应
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
// 只读属性,顶上的 navigationItem
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
// 只读属性,紧挨着 topItem 后面的第一个 navigationItem,navigationBar 执行 pop 后,该 backItem 会变为 topItem
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
// 存放所有 navigationItem 的数组
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
// 设置新的 navigationItem 数组和导航控制器的 setViewControllers: 方法,极其类似
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
// iOS 11 后出现的,navigationBar 加宽,title 字体加粗加大 
@property (nonatomic, readwrite, assign) BOOL prefersLargeTitles UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
prefersLargeTitles.png
// iOS 7 后改变了 tintColor 的定义。它不在影响 navigationBar 的背景,主要影响navigationBar 上按钮的标题颜色
// 给 navigationBar 着色时,使用 barTintColor。
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
// iOS 7 后出现的,设置 navigationBar 的背景颜色,默认是 nil
@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// iOS 7 后出现的,设置 navigationBar 的背景图片
// barMetrics 参数表示屏幕的方向,Default 表示横竖屏用同一张背景图,Compact 表示横屏时用的背景图
// 可以调用该方法两次,分别设置竖屏时的背景图和横屏时的背景图
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// 返回指定 barPosition 和 barMetrics 下的 UIImage (打印的其实是: _UIResizableImage)
- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// UIBarCommon.h 中定义的 UIBarPosition 枚举,用来表示 bar 的位置
typedef NS_ENUM(NSInteger, UIBarPosition) {
    UIBarPositionAny = 0,
    UIBarPositionBottom = 1, // The bar is at the bottom of its local context, and directional decoration draws accordingly (e.g., shadow above the bar).
    UIBarPositionTop = 2, // The bar is at the top of its local context, and directional decoration draws accordingly (e.g., shadow below the bar)
    UIBarPositionTopAttached = 3, // The bar is at the top of the screen (as well as its local context), and its background extends upward—currently only enough for the status bar.
} NS_ENUM_AVAILABLE_IOS(7_0);
// UIBarCommon.h 中定义的 UIBarMetrics 枚举,用来表示 bar  是否设置了 prompt 和 横竖屏的状态的四种组合
typedef NS_ENUM(NSInteger, UIBarMetrics) {
    UIBarMetricsDefault,
    UIBarMetricsCompact,
    UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
    UIBarMetricsCompactPrompt,

    UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact,
    UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt,
};
// 相当于 UIBarPositionAny 下的 -setBackgroundImage:forBarPosition:barMetrics:
// 当 position 是 UIBarPositionTopAttached 的时候,如果需要,图片大小将会垂直拉伸
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// iOS 6 后出现的,默认是 nil,导航条底部的分割线
// 当不为 nil 时,一个自定义的阴影图像代替默认的阴影图像。
// 对于要显示的自定义阴影,自定义背景图片必须通过 -setBackgroundImage:forBarMetrics: 设置。
// 如果使用默认背景图片,则默认的阴影图片也会使用
@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
// iOS 6 后出现的,设置 title 的富文本
@property(nullable,nonatomic,copy) NSDictionary<NSAttributedStringKey, id> *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// iOS 11 后出现的,设置 largeTitle 富文本
@property(nullable, nonatomic, copy) NSDictionary<NSAttributedStringKey, id> *largeTitleTextAttributes UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
// iOS 5 后出现的,设置 title 的垂直偏移,对 largeTitle 无效
// 设置 adjustment  小于 -11 和 大于 32 时控制台打印约束不安全警告
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 返回 title 垂直偏移的大小
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// iOS 7 后出现的,更改 backBarButtonItem 的图片,要两个属性同时修改,只更改一个不起作用
@property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;


二、UINavigationBarDelegate:

@protocol UINavigationBarDelegate <UIBarPositioningDelegate>
// 定义在 UIBarCommon.h 中,
@protocol UIBarPositioning <NSObject> // UINavigationBar, UIToolbar, and UISearchBar conform to this
@property(nonatomic,readonly) UIBarPosition barPosition;
@end

@protocol UIBarPositioningDelegate <NSObject> // UINavigationBarDelegate, UIToolbarDelegate, and UISearchBarDelegate all extend from this
@optional
/* Implement this method on your manual bar delegate when not managed by a UIKit controller.
 UINavigationBar and UISearchBar default to UIBarPositionTop, UIToolbar defaults to UIBarPositionBottom.
 This message will be sent when the bar moves to a window.
 */
// 设置 UIBarPosition
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar;
@end
// 设置能否进行 push, 如果返回 NO 则 navigationBar 不能进行 push
// 但是即使返回 NO,在 viewDidLoad 里面执行的 push 还是能进行,但是后续点击按钮执行 push 则不行
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;
// 如果 push 时有动画,则动画结束回调,如果没有动画,则立即回调
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;
// pop,和上面的 push 逻辑一样
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item; 
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

navigationBar 的内容比较简单,好像没有什么会被日常开发所忽略的。共勉。
end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容