你真的了解UINavigationController嗎?


一:首先查看一下關於UINavigationController的定義

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController

//UINavigationController初始化,自定義NavigationBar,自定義toolbar
- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass NS_AVAILABLE_IOS(5_0);

//UINavigationController初始化,導航控制器的根控制器
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;

//壓棧:將目標控制器壓入棧中
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; 

//彈棧:將棧頂控制器從棧中彈出
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;

//彈棧:彈到指定的目標控制器
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 

//彈棧:彈到根控制器
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;
//導航棧的棧頂視圖 只讀 就是某個導航棧的棧頂視圖,和導航息息相關
@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController; 
//當前顯示的控制器 只讀 visibleViewController和哪個導航棧沒有關系,只是當前顯示的控制器,也就是說任意一個導航的visibleViewController所返回的值應該是一樣的
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController;

//棧里的視圖控制器數組
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;

//替換棧中的視圖控制器數組
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); 

//是否隱藏導航欄
@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;

//設置導航欄隱藏 是否有動畫
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; 

//導航欄
@property(nonatomic,readonly) UINavigationBar *navigationBar; 

//toolbar是否隱藏
@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0);

//toolbar是否隱藏 是否有動畫
- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); 

//toolbar對象
@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0); 

//委托
@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;

//邊緣側滑返回手勢
@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0);

//展示視圖控制器
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0); // Interpreted as pushViewController:animated:

//輸入鍵盤出現時將導航欄隱藏 IOS8特性
@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears NS_AVAILABLE_IOS(8_0);

//滾動頁面時隱藏Bar  IOS8特性
@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe NS_AVAILABLE_IOS(8_0);

//獲取能夠隱藏navigationBar的滑動手勢 只讀
@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer NS_AVAILABLE_IOS(8_0);

//當設置為true時,橫向方向時隱藏NavigationBar
@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact NS_AVAILABLE_IOS(8_0);

//當設置為true時,如果有沒處理的點擊手勢就會隱藏和現實navigationBar 
@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap NS_AVAILABLE_IOS(8_0);

//獲取能夠隱藏navigationBar的點擊手勢 只讀
@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer NS_AVAILABLE_IOS(8_0);

@end

知識點1:UINavigationController  UINavigationBar  UINavigationItem三者的關系

通俗地說就是,UINavigationController是個容器,里面可以裝很多UIViewController。裝這么多UIViewController讓用戶怎么控制它們呢,總得有個工具吧。這個工具就是UINavigationBar。一個容器就這么一個bar,相當於控制台吧。但是,管理那么多UIViewController,控制台上得按鈕啊、標題啊,都千篇一律是不是看起來太無聊了。為了解決這個問題,UINavigationController為每個UIViewController生成一個UINavigationItem,通過這個UINavigationItem可以改變控制台“上面”得按鈕和標題。如果你不自定義UINavigationItem,UINavigationController會使用默認的;

UINavigationController是UIViewController的子類,UINavigationBar是UIView的子類。

UINavigationBar是UINavigationController的一個組成部分,就是上面的那個導航欄。

UINavigationBar又有UINavigationItem組成。

UINavigationItem則有title,按鈕,提示文本等組成,就是我們看到的title文字,右上角的按鈕。 

navigation item在navigation Bar代表一個viewController,具體一點兒來說就是每一個加到navigationController的viewController都會有一個對應的navigationItem一個導航控制器控制多個視圖,NavigationBar上的leftItem,rightItem,title是由當前的視圖控制器控制的

二:UINavigationControllerDelegate的定義內容

/**
 *  將要顯示目標控制器
 *
 *  @param navigationController 當前導航控制器
 *  @param viewController       目標視圖控制器
 *  @param animated             動畫
 */
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
/**
 *  目標控制器最終顯示
 *
 *  @param navigationController 當前導航控制器
 *  @param viewController       目標視圖控制器
 *  @param animated             動畫
 */
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
/**
 *  橫豎屏切換
 *
 *  @param navigationController 當前導航控制器
 *
 *  @return 橫豎屏方向
 */
- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
 *  橫豎屏切換
 *
 *  @param navigationController 當前導航控制器
 *
 *  @return 橫豎屏方向
 */
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
 *  轉場動畫
 *
 *  @param navigationController 當前導航控制器
 *  @param animationController  動畫控制器
 *
 *  @return 轉場動畫
 */
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);
/**
 *  轉場動畫
 *
 *  @param navigationController 當前導航控制器
 *  @param operation            動畫類型枚舉
 *  @param fromVC               起始視圖控制器
 *  @param toVC                 目標視圖控制器
 *
 *  @return 轉場動畫
 */
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

UINavigationController的delegate方法我平時的開發工作中很少用到

三:UIViewController (UINavigationControllerItem)分類

@interface UIViewController (UINavigationControllerItem)
//導航欄上面用戶自定義視圖
@property(nonatomic,readonly,strong) UINavigationItem *navigationItem; 
//推送時隱藏bottom
@property(nonatomic) BOOL hidesBottomBarWhenPushed;
//下級視圖的導航控制器
@property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController; 

@end

知識點1:UINavigationControllerItem因為擴展在UIViewController上,所以可以直接在UIViewController中使用下面的代碼進行調用跟設置;

self.navigationItem.title = @"UINavigationBar使用總結";

四:UIViewController (UINavigationControllerContextualToolbarItems)分類 拓展toolbarItems屬性

@interface UIViewController (UINavigationControllerContextualToolbarItems)
//屬性設置工具條中包含的按鈕
@property (nullable, nonatomic, strong) NSArray<__kindof UIBarButtonItem *> *toolbarItems NS_AVAILABLE_IOS(3_0);
- (void)setToolbarItems:(nullable NSArray<UIBarButtonItem *> *)toolbarItems animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);

@end

五:關於UINavigationBar定義內容

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationBar : UIView <NSCoding, UIBarPositioning> 

@property(nonatomic,assign) UIBarStyle barStyle;

@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;

//Translucent設置成透明度,設置成YES會有一種模糊效果
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; 

//UINavigationBar上面不只是簡單的顯示標題,它也將標題進行了堆棧的管理,每一個標題抽象為的對象在iOS系統中是UINavigationItem對象,我們可以通過push與pop操作管理item組。
//向棧中添加一個item,上一個item會被推向導航欄的左側,變為pop按鈕,會有一個動畫效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一個item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; 
//當前push到最上層的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//僅次於最上層的item,一般式被推向導航欄左側的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//獲取堆棧中所有item的數組
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//設置一組item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated; 

//系統類型按鈕文字顏色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;

//通過barTintColor來設置背景色
@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;  

//設置工具欄背景和陰影圖案
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

//通過背景圖片來設置導航欄的外觀
- (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;

//背景陰影圖片 - 即分割線
@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;

//標題的富文本
@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

//標題垂直偏移
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

//設置返回按鈕的圖片
@property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

@end

UINavigationBar是用於實現管理層級關系內容的組件,直接繼承自UIView。通常用在UINavgationController類中,用於管理和顯示UINavgationController的subViewController , 同時UINavgationBar也可以單獨使用,添加至任何的UIView中。UINavigationBar比較重要的屬性為,左側按鈕,中間的標題,以及右側按鈕。在平時的應用程序中,我們常常使用到自定義UINavigationBar來完成導航條的設置。

知識點1:導航欄全局屬性設置

//全局設置導航欄主題
- (void)setNavigationControllerAppearance {
    [UINavigationBar appearance].barStyle  = UIBarStyleBlack;
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithWhite:0.1 alpha:0.5]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}

全局設置導航欄的好處有兩個:一是不用對每個NavigationBar進行設置;二是方便做主題管理,切換主題,只需要更改全局設置即可。

知識點2:自定義返回的圖標並去除文字,要同時設置setBackIndicatorImage、setBackIndicatorTransitionMaskImage

[self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"navibar_back_btn_bg_normal.png"]];  
  
[self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"navibar_back_btn_bg_normal.png"]];   
  
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];  

self.navigationItem.backBarButtonItem = backItem;  

知識點3:設置導航條底部線條的顏色

顏色轉圖片的代碼:

@implementation UIImage (ColorImage)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end


設置導航欄底部線條顏色的代碼:

    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar setBackgroundImage:[[UIImage alloc] init]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    //此處使底部線條顏色為紅色
    [navigationBar setShadowImage:[UIImage imageWithColor:[UIColor redColor]]];

知識點4:隱藏導航條底部線條

//找查到Nav底部的黑線
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

然后設置這個UIImageView為隱藏就可以了;

方法二:這種主要是隱藏后沒辦法再顯示出來

   UINavigationBar *navigationBar = self.navigationController.navigationBar;
    //設置透明的背景圖,便於識別底部線條有沒有被隱藏
    [navigationBar setBackgroundImage:[[UIImage alloc] init]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    //此處使底部線條失效
    [navigationBar setShadowImage:[UIImage new]];

六:關於UINavigationItem的定義

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationItem : NSObject <NSCoding>

- (instancetype)initWithTitle:(NSString *)title NS_DESIGNATED_INITIALIZER;

- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;

//設置導航欄中間的內容標題
@property(nullable, nonatomic,copy)   NSString        *title;         
//設置導航欄中間的內容視圖    
@property(nullable, nonatomic,strong) UIView          *titleView;        

@property(nullable,nonatomic,copy)   NSString *prompt;     
//返回
@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem; 
//是否隱藏返回Button
@property(nonatomic,assign) BOOL hidesBackButton;

- (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated;
//左邊數組Item
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems NS_AVAILABLE_IOS(5_0);
//右邊數組Item
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems NS_AVAILABLE_IOS(5_0);

- (void)setLeftBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);

- (void)setRightBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);

//通過指定該屬性為YES,可以讓leftBarButtonItem和backBarButtonItem同時顯示,其中leftBarButtonItem顯示在backBarButtonItem的右邊 默認值為NO
@property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0);
//左邊Item
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
//右邊Item
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;

- (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;

- (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;

@end

UINavigationController會為每一個入棧的UIViewController生成一個UINavigationItem. UIViewController通過修改UINavigationItem可以控制UINavigationBar上的按鈕和標題等

知識點1:關於UINavigationItem內容

你可以通過設置self.navigationItem.leftBarButtonItem為某個ButtonItem,
self.navigationItem.leftBarButtonItem
self.navigationItem.rightBarButtonItem
self.navigationItem.backBarButtonItem
self.navigationItem.titleView等等
注:1、這里的self 指的是UIViewController。
2、如果你在新視圖中不修改backBarButtonItem 或leftBarButtonItem UINavigationController 會自動添加左邊返回按鈕用以返回了一個視圖。總體的顯示原則如下:
1)、Left side of the navigationBar 左按鈕
a)如果當前的viewController設置了leftBarButtonItem,則顯示當前VC所自帶的leftBarButtonItem。
b)如果當前的viewController沒有設置leftBarButtonItem,且當前VC不是rootVC的時候,則顯示前一層VC的backBarButtonItem。如果前一層的VC沒有顯示的指定backBarButtonItem的話,系統將會根據前一層VC的title屬性自動生成一個back按鈕,並顯示出來。
c)如果當前的viewController沒有設置leftBarButtonItem,且當前VC已是rootVC的時候,左邊將不顯示任何東西。
此處注意:5.0中新增加了一個屬性leftItemsSupplementBackButton,通過指定該屬性為YES,可以讓leftBarButtonItem和backBarButtonItem同時顯示,其中leftBarButtonItem顯示在backBarButtonItem的右邊。
2)、title 標題
a)如果當前VC通過 .navigationItem.titleView指定了自定義的titleView,系統將會顯示指定的titleView,此處要注意自定義titleView的高度不要超過navigationBar的高度,否則會顯示出界。
b)如果當前VC沒有指定titleView,系統則會根據當前VC的title或者當前VC的navigationItem.title的內容創建一個UILabel並顯示,其中如果指定了navigationItem.title的話,則優先顯示navigationItem.title的內容。
3)、Right side of the navigationBar  右按鈕
a)如果當前VC指定了rightBarButtonItem的話,則顯示指定的內容。
b)如果當前VC沒有指定rightBarButtonItem的話,則不顯示任何東西。

知識點2:相關屬性運用

NavigationBar設置中間的標題或者自定義View
[self.navigationItem setTitle:@"旅行"];
[self.navigationItem setTitleView:[UIImage imageNamed:@"圖片名稱"];

單個或多個左右Item設置:單個leftItem設置:
UIBarButtonItem  *leftBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"BackIcon"] style:UIBarButtonItemStylePlain target:self action:nil];
 [leftBarButton setTintColor:[UIColor colorWithWhite:0 alpha:1]]; 
self.navigationItem.leftBarButtonItem = leftBarButton;

多個rightItem設置:
UIBarButtonItem *firstItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil];
UIBarButtonItem *secondItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:nil];
NSArray *rightItems = @[firstItem, secondItem];
self.navigationItem.rightBarButtonItems = rightItems;

知識點3:設置偏移值

// 返回按鈕內容左靠
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 讓返回按鈕內容繼續向左邊偏移10
button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

七:UIBarButtonItem定義內容

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarButtonItem : UIBarItem <NSCoding>

- (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); 
- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;
//自定義視圖
- (instancetype)initWithCustomView:(UIView *)customView;

//樣式
@property(nonatomic)         UIBarButtonItemStyle style;            // default is UIBarButtonItemStylePlain
@property(nonatomic)         CGFloat              width;           
@property(nullable, nonatomic,copy)    NSSet<NSString *>   *possibleTitles;   // default is nil
//自定義視圖
@property(nullable, nonatomic,strong)  __kindof UIView     *customView;       
@property(nullable, nonatomic)         SEL                  action;           // default is NULL
@property(nullable, nonatomic,weak)    id                   target;           // default is nil

- (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;


- (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;

@property(nullable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);


- (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;


- (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;

- (void)setBackButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backButtonBackgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

- (void)setBackButtonTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (UIOffset)backButtonTitlePositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;


- (void)setBackButtonBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (CGFloat)backButtonBackgroundVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

@end

知識點1UIBarButtonSystemItem的樣式介紹

typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
    UIBarButtonSystemItemDone,//顯示完成
    UIBarButtonSystemItemCancel,//顯示取消
    UIBarButtonSystemItemEdit,  //顯示編輯
    UIBarButtonSystemItemSave, //顯示保存 
    UIBarButtonSystemItemAdd,//顯示加號
    UIBarButtonSystemItemFlexibleSpace,//什么都不顯示,占位一個空間位置
    UIBarButtonSystemItemFixedSpace,//和上一個類似
    UIBarButtonSystemItemCompose,//顯示寫入按鈕
    UIBarButtonSystemItemReply,//顯示循環按鈕
    UIBarButtonSystemItemAction,//顯示活動按鈕
    UIBarButtonSystemItemOrganize,//顯示組合按鈕
    UIBarButtonSystemItemBookmarks,//顯示圖書按鈕
    UIBarButtonSystemItemSearch,//顯示查找按鈕
    UIBarButtonSystemItemRefresh,//顯示刷新按鈕
    UIBarButtonSystemItemStop,//顯示停止按鈕
    UIBarButtonSystemItemCamera,//顯示相機按鈕
    UIBarButtonSystemItemTrash,//顯示移除按鈕
    UIBarButtonSystemItemPlay,//顯示播放按鈕
    UIBarButtonSystemItemPause,//顯示暫停按鈕
    UIBarButtonSystemItemRewind,//顯示退后按鈕
    UIBarButtonSystemItemFastForward,//顯示前進按鈕
    UIBarButtonSystemItemUndo,//顯示消除按鈕
    UIBarButtonSystemItemRedo ,//顯示重做按鈕
    UIBarButtonSystemItemPageCurl ,//在tool上有效
};
例如:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];


- (void)right:(id)sender
{
    NSLog(@"rightBarButtonItem");
}

知識點2自定義UIView的UIBarButtonItem

 UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 60)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];

 

 

最近有個妹子弄的一個關於擴大眼界跟內含的訂閱號,每天都會更新一些深度內容,在這里如果你感興趣也可以關注一下(嘿對美女跟知識感興趣),當然可以關注后輸入:github 會有我的微信號,如果有問題你也可以在那找到我;當然不感興趣無視此信息;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM