iOS_UI开发中容易被忽略的细节之--UIToolbar.h & UIBarButtonItem.h & UIBarItem.h

前言

UIToolbar 第一次接触是因为 导航控制器的 toolbarHidden 和 toolbar 属性。

目录

    1. UIToolbar 的属性和方法
    1. UIBarItem 的属性和方法
    1. UIBarButtonItem 的属性和方法

一、UIToolbar 的属性和方法

// 定义中规中矩,继承 UIView,遵守 UIBarPositioning 协议。
NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIToolbar : UIView 
// 很眼熟的属性,和 UINavigationBar 一模一样
@property(nonatomic) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; // default is UIBarStyleDefault
// 如果是 navigationController.toolbar,UIToolbar 受 UINavigationController 控制时,设置 方法无效,需要使用 UIViewController (UINavigationControllerContextualToolbarItems) 中的 toolbarItems 的设置方法
// 设置或读取 显示 UIBarButtonItem,默认是 nil,更改没有动画,在 UIToolbar 上按顺序显示
@property(nullable, nonatomic, copy) NSArray *items;
// 和 UINavigationBar 一模一样的属性,是否透明
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;
// 如果是 navigationController.toolbar,UIToolbar 受 UINavigationController 控制时,设置 方法无效,需要使用 UIViewController (UINavigationControllerContextualToolbarItems) 中的 toolbarItems 的设置方法
// 如果 animated 为 YES,将淡入淡出的重新排列和调整间距
- (void)setItems:(nullable NSArray *)items animated:(BOOL)animated;
// 按钮的 渲染颜色
@property(null_resettable, nonatomic, strong) UIColor *tintColor;
// UIToolbar 的背景颜色
@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// iOS 5 后出现的, 设置背景图片,barStyle、tintColor、backgroundImage 会相互影响,
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// iOS 6 后出现的,设置阴影图片
- (void)setShadowImage:(nullable UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
// iOS 7 后出现的,当 UIToolbar 由 UINavigationController  控制时也许不需要设置代理对象
@property(nullable, nonatomic, weak) id delegate NS_AVAILABLE_IOS(7_0);

二、UIBarItem 的属性和方法

该类的内容比较少,只做简要分析。

// 继承自 NSObject,遵守 NSCoding 和 UIAppearance 协议
// 这里的 UIAppearance 后面再详细分析
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject 
// 两个指定的初始化方法,大家耳熟能详
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
// 是否激活,即是否可点击,默认为 YES
@property(nonatomic,getter=isEnabled) BOOL         enabled;      // default is YES
// 标题,默认为 nil
@property(nullable, nonatomic,copy)             NSString    *title;        // default is nil
// 图片,默认为 nil
@property(nullable, nonatomic,strong)           UIImage     *image;        // default is nil
// iOS 5 后出现的,横屏下的图片,默认是 nil
@property(nullable, nonatomic,strong)           UIImage     *landscapeImagePhone NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // default is nil
// iOS 11 后出现的,标准图像的高分辨率版本,默认是 nil,
// 用于渲染辅助 UI,例如对于需要大文本的视障人士使用。如果没有提供,系统可能会尝试基于标准的图像去生成图像。
// (例如,通过以更高的分辨率光栅化匹配的PDF 表示法在更高的一个分辨率下)
// 此处体现了苹果的人文关怀
@property(nullable, nonatomic,strong)           UIImage     *largeContentSizeImage API_AVAILABLE(ios(11.0));
// Image 的缩进,默认是 UIEdgeInsetsZero
@property(nonatomic)                  UIEdgeInsets imageInsets;
// iOS 5 后出现的,横屏下的图片缩进,默认也是 UIEdgeInsetsZero,当然只有在 landscapeImagePhone 有值时该属性才有效
@property(nonatomic)                  UIEdgeInsets landscapeImagePhoneInsets NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
// iOS 11 后出现的,largeContentSizeImage 的缩进,同上
@property(nonatomic)                  UIEdgeInsets largeContentSizeImageInsets API_AVAILABLE(ios(11.0));
// tag 值,默认是 0
@property(nonatomic)                  NSInteger    tag;
// iOS 5 后出现的,设置某个 state 下的 title 的属性
- (void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 获取某个 state 下的 title 的属性
- (nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

三、UIBarButtonItem 的属性和方法

// 首先是定义,继承自 UIBarItem,遵守 NSCoding 属性
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarButtonItem : UIBarItem 
// 全部的初始化方法,可能我们对 title 和 image 初始化比较熟悉了,这里主要看最后的两个初始化方法
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
- (instancetype)initWithImage:(nullable UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action NS_AVAILABLE_IOS(5_0); // landscapeImagePhone will be used for the bar button image when the bar has Compact or Condensed bar metrics.
- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
// 使用系统提供的 UIBarButtonSystemItem 创建 UIBarButtonItem 
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;
// 使用 一个自定义 view 创建 UIBarButtonItem
- (instancetype)initWithCustomView:(UIView *)customView;
// 首先看一下 Plain 和 Done 的区别,Done 的字体比 Plain 粗一些
typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {
    UIBarButtonItemStylePlain,
    UIBarButtonItemStyleBordered NS_ENUM_DEPRECATED_IOS(2_0, 8_0, "Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later"),
    UIBarButtonItemStyleDone,
};
iOS_UI开发中容易被忽略的细节之--UIToolbar.h & UIBarButtonItem.h & UIBarItem.h_第1张图片
plainAndDone.png
// 系统提供的样式,且 UIBarButtonItemStyle 默认是 Plain,字体较细一些的样式
typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
    UIBarButtonSystemItemDone, // title 是 Done 
    UIBarButtonSystemItemCancel, // title 是 Cancel 
    UIBarButtonSystemItemEdit, // title 是 Edit 
    UIBarButtonSystemItemSave, // title 是 Save
    UIBarButtonSystemItemAdd, // image 是一个加号的图片
    UIBarButtonSystemItemFlexibleSpace, // 空白占位按钮,不能单独使用,要给和其它 barItem 一起放在数组里面,灵活的间隔
    UIBarButtonSystemItemFixedSpace, // 同上,固定间隔
    UIBarButtonSystemItemCompose, // image 是一个编辑的图片
    UIBarButtonSystemItemReply, // image  是一个回答的图片
    UIBarButtonSystemItemAction, // image 是一个分析的图片
    UIBarButtonSystemItemOrganize, // image 是一个文件夹的图片
    UIBarButtonSystemItemBookmarks, // image 是一个书本展开的图片
    UIBarButtonSystemItemSearch, // image 是一个搜索的图片
    UIBarButtonSystemItemRefresh, // image 是一个刷新的图片
    UIBarButtonSystemItemStop, // image 是一个 X 的图片
    UIBarButtonSystemItemCamera, // image 是一个相机的图片
    UIBarButtonSystemItemTrash, // image 是一个垃圾桶的图片
    UIBarButtonSystemItemPlay, // image 是一个播放按钮的图片
    UIBarButtonSystemItemPause, // image 是一个暂停按钮的图片
    UIBarButtonSystemItemRewind, // image 是一个回退的图片
    UIBarButtonSystemItemFastForward, // image 是一个前进的图片
    UIBarButtonSystemItemUndo NS_ENUM_AVAILABLE_IOS(3_0), // title 是 Undo
    UIBarButtonSystemItemRedo NS_ENUM_AVAILABLE_IOS(3_0), // title 是 Redo
    UIBarButtonSystemItemPageCurl NS_ENUM_DEPRECATED_IOS(4_0, 11_0) // 废弃
};
// 默认是 UIBarButtonItemStylePlain
@property(nonatomic)         UIBarButtonItemStyle style;
// 宽度,默认是 0
@property(nonatomic)         CGFloat              width;            // default is 0.0
// 可能的标题集合,没看懂这个属性是啥意思
@property(nullable, nonatomic,copy)    NSSet   *possibleTitles;   // default is nil
// 自定义 View,当使用自定义 View 创建 UIBarButtonItem 时,指向该自定义 View  
@property(nullable, nonatomic,strong)  __kindof UIView     *customView;       // default is nil
// 点击方法
@property(nullable, nonatomic)         SEL                  action;           // default is NULL
// 目标对象
@property(nullable, nonatomic,weak)    id                   target;           // default is nil
// iOS 5 后出现的,设置背景图片,可指定状态和屏幕方向
// 外观修饰符
// 通过 [UIBarButtonItem appearance] 代理定制所有按钮,或者用一个按钮实例调用该方法定制一个特殊的按钮,它也能用于 UINavigationItem 和 UIToolbar 上所有的按钮上
// 一般来说,应为没有自定义值集的其他状态指定正常状态的值
// 同样,要针对横竖屏下按钮宽度和高度不同做调整
// 为按钮的任何样式设置图片
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// iOS 6 后出现的,添加 UIBarButtonItemStyle 参数
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state style:(UIBarButtonItemStyle)style barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForState:(UIControlState)state style:(UIBarButtonItemStyle)style barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
// iOS 5 后出现的,渲染颜色
@property(nullable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);
// iOS 5 后出现的,设置 Background 垂直方向的偏移量,image 不为 nil 时有效,title 不为 nil 时无效,貌似针对有 image 的按钮
- (void)setBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 
- (CGFloat)backgroundVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
iOS_UI开发中容易被忽略的细节之--UIToolbar.h & UIBarButtonItem.h & UIBarItem.h_第2张图片
adjustment_0.png
iOS_UI开发中容易被忽略的细节之--UIToolbar.h & UIBarButtonItem.h & UIBarItem.h_第3张图片
adjustment_5.png
// iOS 5 后出现的,调整 title 的位置,调整水平方向的位置,左右偏移量,垂直方向无效
- (void)setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 
- (UIOffset)titlePositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 定义在 UIGeometry.h 里面的结构体
typedef struct UIOffset {
    CGFloat horizontal, vertical; // specify amount to offset a position, positive for right or down, negative for left or up
} UIOffset;
// iOS 5 后出现的,只针对返回按钮的设置方法,该按钮位于 UINavigationItem 上   `@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem __TVOS_PROHIBITED; // Bar button item to use for the back button in the child navigation item.`
// 设置背景图片
- (void)setBackButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
- (nullable UIImage *)backButtonBackgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 设置 title 的垂直偏移
- (void)setBackButtonTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
- (UIOffset)backButtonTitlePositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 设置 Background 的垂直偏移
- (void)setBackButtonBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
- (CGFloat)backButtonBackgroundVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 只针对 iOS 的  SpringLoading 分类,且遵守 UISpringLoadedInteractionSupporting
#if TARGET_OS_IOS
@interface UIBarButtonItem (SpringLoading) 
@end
#endif
// iOS 11 后出现的,定义在 UISpringLoadedInteractionSupporting.h 中,弹簧作用
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos)
@protocol UISpringLoadedInteractionSupporting 

@property (nonatomic, assign, getter=isSpringLoaded) BOOL springLoaded UIKIT_AVAILABLE_IOS_ONLY(11_0);

@end

到这里就结束,相关的 .h 文件都看完了,是否对导航栏有一个全面的认识了呢,对后面的完全自定义导航栏有什么启发呢。emmm...
end

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