UINavigationController,UINavigationBar,UIBarButtonItem

1.UINavigationController繼承自UIViewController

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIViewController.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIInterface.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIPanGestureRecognizer.h>
#import <UIKit/UITapGestureRecognizer.h>

UINavigationController管理堆棧視圖控制器和一個導航欄。它執(zhí)行水平視圖轉(zhuǎn)換為推動的導航欄和彈出視圖,同時保持同步。大多數(shù)客戶不需要子類UINavigationController。如果導航控制器是嵌套在tabbar控制器,它使用標題和底部的工具欄屬性視圖控制器在堆棧上。UINavigationController是可旋轉(zhuǎn)的前視圖控制器是否可旋轉(zhuǎn)。導航控制器之間不均勻rotatability目前不支持。

UINavigationControllerOperation操作方式

UINavigationControllerOperationNone,---none
UINavigationControllerOperationPush,--push
 UINavigationControllerOperationPop,--pop

UINavigationControllerHideShowBarDuration---CGFloat

導航欄出現(xiàn)或者消失所用時間

導航控制器相關(guān)

@class UIView, UINavigationBar, UINavigationItem, UIToolbar;
@protocol UINavigationControllerDelegate;

導航控制器的方法

/* Use this initializer to make the navigation controller use your custom bar class.
Passing nil for navigationBarClass will get you UINavigationBar, nil for toolbarClass gets UIToolbar.
The arguments must otherwise be subclasses of the respective UIKit classes.
*/
使用這個初始化,可以自定義導航欄視圖
通過nil navigationBarClass將讓你獲得UINavigationBar
通過nil navigationBarClass將讓你獲得toolbarClass

- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass NS_AVAILABLE_IOS(5_0);

// Convenience method pushes the root view controller without animation.
導航控制器跳轉(zhuǎn)到自己的跟視圖,沒有動畫---啥??也看不出來

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; 

// Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
使用水平滑動過渡。如果視圖控制器已經(jīng)在堆棧中,沒有影響的。---啥子意思??
就是簡單的push,后面啥意思------????

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; 

// Returns the popped controller.
回到上一級控制器

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; 

// Pops view controllers until the one specified is on top. Returns the popped controllers.

返回到一個指定的視圖控制器-----push太多了

- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 

// Pops until there's only a single view controller left on the stack. Returns the popped controllers.
返回到導航控制器的第一個控制器,就是rootConreoller

- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated; 

導航控制器的其他屬性

//棧頂?shù)目刂破?@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController; // The top view controller on the stack.
//模態(tài)控制器存在,返回該控制器,否則,返回棧頂?shù)目刂破?有資料這樣寫--
//當前顯示的控制器 只讀 visibleViewController和哪個導航棧沒有關(guān)系,只是當前顯示的控制器,也就是說任意一個導航的visibleViewController所返回的值應該是一樣的
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.
//當前導航控制器棧里頭的控制器數(shù)組
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; // The current view controller stack.

方法

// If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.

/ /如果動畫是肯定的,那么模擬推動或者流行取決于新的頂部視圖控制器在堆棧之前。
有資料這樣寫--
//替換棧中的視圖控制器數(shù)組---同意,媽蛋,翻譯下來怎么不一樣的這么厲害,我太菜??過四級了呀??!
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); 
導航欄是否隱藏
@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;

設(shè)置導航欄消失或者出現(xiàn),是否以動畫的形式

// Hide or show the navigation bar. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; 

// The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.
控制器管理navigation bar ,push,pop,或者設(shè)置navigation items,navigationBar都不支持

@property(nonatomic,readonly) UINavigationBar *navigationBar; 

// Defaults to YES, i.e. hidden.
//toolbar是否隱藏

@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; 

// Hide or show the toolbar at the bottom of the screen. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.
隱藏或顯示屏幕的底部的工具欄。如果動畫,它將使用UINavigationControllerHideShowBarDuration這個時間垂直過渡。

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; 

// For use when presenting an action sheet.
/ /當呈現(xiàn)一個動作表單使用。

@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; 

//委托

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

//邊緣側(cè)滑返回手勢

@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

//展示視圖控制器

- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0); // Interpreted as pushViewController:animated:

/ / /鍵盤出現(xiàn)的時候,導航控制器的navigationBar工具欄會隱藏。在鍵盤dismiss后,兩個bar依然Hidden,但是在內(nèi)容區(qū)域點擊,他們就又出來了---IOS8特性

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

/// When the user swipes, the navigation controller's navigationBar & toolbar will be hidden (on a swipe up) or shown (on a swipe down). The toolbar only participates if it has items.
導航欄,在用戶向上刷的時候Hidden,在向下刷NotHidden,至于工具欄應該是有內(nèi)容的時候才參與吧。。。---我猜的------IOS8特性

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

/// The gesture recognizer that triggers if the bars will hide or show due to a swipe. Do not change the delegate or attempt to replace this gesture by overriding this method.
//獲取能夠隱藏navigationBar的滑動手勢 只讀

@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

/// When the UINavigationController's vertical size class is compact, hide the UINavigationBar and UIToolbar. Unhandled taps in the regions that would normally be occupied by these bars will reveal the bars.
//當設(shè)置為true時,橫向方向時隱藏NavigationBar

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

/// When the user taps, the navigation controller's navigationBar & toolbar will be hidden or shown, depending on the hidden state of the navigationBar. The toolbar will only be shown if it has items to display.
//當設(shè)置為true時,如果有沒處理的點擊手勢就會隱藏和顯示navigationBar

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

/// The gesture recognizer used to recognize if the bars will hide or show due to a tap in content. Do not change the delegate or attempt to replace this gesture by overriding this method.
//獲取能夠隱藏navigationBar的點擊手勢 只讀

@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;


知識點1:UINavigationController UINavigationBar UINavigationItem三者的關(guān)系
通俗地說就是,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的定義內(nèi)容

/**
 *  將要顯示目標控制器
 *
 *  @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;
/**
 *  轉(zhuǎn)場動畫
 *
 *  @param navigationController 當前導航控制器
 *  @param animationController  動畫控制器
 *
 *  @return 轉(zhuǎn)場動畫
 */
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);
/**
 *  轉(zhuǎn)場動畫
 *
 *  @param navigationController 當前導航控制器
 *  @param operation            動畫類型枚舉
 *  @param fromVC               起始視圖控制器
 *  @param toVC                 目標視圖控制器
 *
 *  @return 轉(zhuǎn)場動畫
 */
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

三: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中使用下面的代碼進行調(diào)用跟設(shè)置;
self.navigationItem.title = @"UINavigationBar使用總結(jié)";


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

@interface UIViewController (UINavigationControllerContextualToolbarItems)
//屬性設(shè)置工具條中包含的按鈕
@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

五:關(guān)于UINavigationBar定義內(nèi)容

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

@property(nonatomic,assign) UIBarStyle barStyle;

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

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

//UINavigationBar上面不只是簡單的顯示標題,它也將標題進行了堆棧的管理,每一個標題抽象為的對象在iOS系統(tǒng)中是UINavigationItem對象,我們可以通過push與pop操作管理item組。
//向棧中添加一個item,上一個item會被推向?qū)Ш綑诘淖髠?cè),變?yōu)閜op按鈕,會有一個動畫效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一個item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; 
//當前push到最上層的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//僅次于最上層的item,一般式被推向?qū)Ш綑谧髠?cè)的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//獲取堆棧中所有item的數(shù)組
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//設(shè)置一組item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated; 

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

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

//設(shè)置工具欄背景和陰影圖案
- (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;

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

//設(shè)置返回按鈕的圖片
@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是用于實現(xiàn)管理層級關(guān)系內(nèi)容的組件,直接繼承自UIView。通常用在UINavgationController類中,用于管理和顯示UINavgationController的subViewController , 同時UINavgationBar也可以單獨使用,添加至任何的UIView中。UINavigationBar比較重要的屬性為,左側(cè)按鈕,中間的標題,以及右側(cè)按鈕。在平時的應用程序中,我們常常使用到自定義UINavigationBar來完成導航條的設(shè)置。


知識點1:導航欄全局屬性設(shè)置

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

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

知識點2:自定義返回的圖標并去除文字,要同時設(shè)置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:設(shè)置導航條底部線條的顏色

顏色轉(zhuǎn)圖片的代碼:

@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


設(shè)置導航欄底部線條顏色的代碼:

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

然后設(shè)置這個UIImageView為隱藏就可以了;


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

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

六:關(guān)于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;

//設(shè)置導航欄中間的內(nèi)容標題
@property(nullable, nonatomic,copy)   NSString        *title;         
//設(shè)置導航欄中間的內(nèi)容視圖    
@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;
//左邊數(shù)組Item
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems NS_AVAILABLE_IOS(5_0);
//右邊數(shù)組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同時顯示,其中l(wèi)eftBarButtonItem顯示在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:關(guān)于UINavigationItem內(nèi)容

你可以通過設(shè)置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設(shè)置了leftBarButtonItem,則顯示當前VC所自帶的leftBarButtonItem。
b)如果當前的viewController沒有設(shè)置leftBarButtonItem,且當前VC不是rootVC的時候,則顯示前一層VC的backBarButtonItem。如果前一層的VC沒有顯示的指定backBarButtonItem的話,系統(tǒng)將會根據(jù)前一層VC的title屬性自動生成一個back按鈕,并顯示出來。
c)如果當前的viewController沒有設(shè)置leftBarButtonItem,且當前VC已是rootVC的時候,左邊將不顯示任何東西。
此處注意:5.0中新增加了一個屬性leftItemsSupplementBackButton,通過指定該屬性為YES,可以讓leftBarButtonItem和backBarButtonItem同時顯示,其中l(wèi)eftBarButtonItem顯示在backBarButtonItem的右邊。
2)、title 標題
a)如果當前VC通過 .navigationItem.titleView指定了自定義的titleView,系統(tǒng)將會顯示指定的titleView,此處要注意自定義titleView的高度不要超過navigationBar的高度,否則會顯示出界。
b)如果當前VC沒有指定titleView,系統(tǒng)則會根據(jù)當前VC的title或者當前VC的navigationItem.title的內(nèi)容創(chuàng)建一個UILabel并顯示,其中如果指定了navigationItem.title的話,則優(yōu)先顯示navigationItem.title的內(nèi)容。
3)、Right side of the navigationBar  右按鈕
a)如果當前VC指定了rightBarButtonItem的話,則顯示指定的內(nèi)容。
b)如果當前VC沒有指定rightBarButtonItem的話,則不顯示任何東西。

知識點2:相關(guān)屬性運用

NavigationBar設(shè)置中間的標題或者自定義View
[self.navigationItem setTitle:@"旅行"];
[self.navigationItem setTitleView:xxxx];

單個或多個左右Item設(shè)置:單個leftItem設(shè)置:
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設(shè)置:
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:設(shè)置偏移值

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

七:UIBarButtonItem定義內(nèi)容

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

知識點1:UIBarButtonSystemItem的樣式介紹

typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
    UIBarButtonSystemItemDone,//顯示完成
    UIBarButtonSystemItemCancel,//顯示取消
    UIBarButtonSystemItemEdit,  //顯示編輯
    UIBarButtonSystemItemSave, //顯示保存 
    UIBarButtonSystemItemAdd,//顯示加號
    UIBarButtonSystemItemFlexibleSpace,//什么都不顯示,占位一個空間位置
    UIBarButtonSystemItemFixedSpace,//和上一個類似
    UIBarButtonSystemItemCompose,//顯示寫入按鈕
    UIBarButtonSystemItemReply,//顯示循環(huán)按鈕
    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];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容