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 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 *items;
// 设置新的 navigationItem 数组和导航控制器的 setViewControllers: 方法,极其类似
- (void)setItems:(nullable NSArray *)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 *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// iOS 11 后出现的,设置 largeTitle 富文本
@property(nullable, nonatomic, copy) NSDictionary *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 
// 定义在 UIBarCommon.h 中,
@protocol UIBarPositioning  // UINavigationBar, UIToolbar, and UISearchBar conform to this
@property(nonatomic,readonly) UIBarPosition barPosition;
@end

@protocol UIBarPositioningDelegate  // 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 )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

你可能感兴趣的:(iOS_UI开发中容易被忽略的细节之--UINavigationBar.h)