iOS-自定義導航條

前言

隨著項目功能的需要魔眨,可能有些界面的導航條比較特殊媳维,比如

  • 需要添加不同的按鈕,比如掃碼遏暴,心愿單侄刽,分享,更多等等朋凉。
  • 需要將菜單替換成圖片
  • 需要隱藏整個導航條
  • 需要隨著滾動州丹,導航條背景跟著變色
  • 隱藏導航條底部的線條
  • ...
    所以自定義一個導航條就顯得至關重要了。
效果圖
addGes.gif
實現(xiàn)思路
  • 1.自定義一個導航條視圖 NaviBarView,并且對外提供各種方法可以動態(tài)修改要展示的內(nèi)容墓毒。
  • 2.定義一個 VC 的基類吓揪,項目中所有的 VC 都繼承它。在它里面創(chuàng)建并且維護一個導航條所计,隱藏系統(tǒng)自帶的導航條磺芭,并且對外提供各種方法。

部分相關代碼如下

1.NaviBarView類的實現(xiàn)
  • NaviBarView.h
#import <UIKit/UIKit.h>
@class BaseViewController;

#define StatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define NavBarHeight 44
#define NavHeight (StatusBarHeight + NavBarHeight)
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

/**
 導航條
 */
@interface NaviBarView : UIView

@property (retain, nonatomic) UIView *statusBar;    // 狀態(tài)欄
@property (retain, nonatomic) UIView *navigationBar;    // 導航條
@property (retain, nonatomic) UIButton *rightWishBtn;   // 右側分享按鈕
@property (retain, nonatomic) UIButton *leftMenuBtn;    // 左側菜單欄
@property (retain, nonatomic) UIButton *backBtn;    // 返回按鈕
@property (retain, nonatomic) UILabel *titleLabel;  // 標題
@property(nonatomic, strong)UIView *lineView;   // 底部分割線

- (instancetype)initWithController:(BaseViewController *)controller;

// 掃碼和心愿單
- (void)addScanAndWishList;
// 添加返回按鈕
- (void)addBackBtn;
// 添加底部分割線
- (void)addBottomSepLine;
// 設置標題
- (void)setNavigationTitle:(NSString *)title;
// 設置導航條透明
- (void)clearNavBarBackgroundColor;

// 右側添加按鈕
- (UILabel *)addRightMenu:(NSString *)actionName withAction:(SEL)action;

@end
  • NaviBarView.m初始化
// 初始化
- (instancetype)initWithController:(BaseViewController *)controller {
    _controller = controller;
    
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, NavHeight)];
    self.backgroundColor = [UIColor clearColor];
    self.layer.zPosition = 999999;
    
    // 最頂部的狀態(tài)欄
    _statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, StatusBarHeight)];
    _statusBar.backgroundColor = [UIColor whiteColor];
    [self addSubview:_statusBar];
    
    _navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, StatusBarHeight, ScreenWidth, NavBarHeight)];
    _navigationBar.backgroundColor = [UIColor whiteColor];
    [self addSubview:_navigationBar];

    return self;
}
  • NavBarView.m添加視圖 - 只羅列部分代碼
// 返回按鈕
- (void)addBackBtn {
    // 不能添加多個
    UIView *srcBack = [_navigationBar viewWithTag:TagBackBtn];
    if (srcBack)
        return;
    
    UIImage *background = [[UIImage imageNamed:@"nav_back_black"] ifRTLThenOrientationUpMirrored];
    UIImage *backgroundOn = [[UIImage imageNamed:@"nav_back_black"] ifRTLThenOrientationUpMirrored];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setAccessibilityIdentifier:@"TopBackBtn"];
    button.tag = TagBackBtn;
    [button onTap:self action:@selector(doBackPrev)];
    
    [button setImage:background forState:UIControlStateNormal];
    [button setImage:backgroundOn forState:UIControlStateHighlighted];
    
    button.frame = CGRectMake(0, 0, 60, 44);
    button.contentEdgeInsets = UIEdgeInsetsMake(7, 7, 7, 20);
    [_navigationBar addSubview:button];
    [button rtlToParent];
    _backBtn = button;
}

// 添加頂部右邊的文字類的按鈕
- (UILabel *)addRightMenu:(NSString *)actionName withAction:(SEL)action {
    
    // 當重復添加時,移除舊的按鈕
    UILabel *srcLabel = (UILabel *)[_navigationBar viewWithTag:TagRightMenu];
    if (srcLabel) {
        [srcLabel removeFromSuperview];
    }
    
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 110, 0, 100, 44)];
    lb.backgroundColor = [UIColor clearColor];
    lb.font = [UIFont systemFontOfSize:16];
    lb.textColor = [UIColor blackColor];
    lb.text = actionName;
    lb.userInteractionEnabled = YES;
    lb.textAlignment = NSTextAlignmentRight;
    lb.tag = TagRightMenu;
    [lb onTap:_controller action:action];
    [_navigationBar addSubview:lb];
    [lb rtlToParent];
    _rightMenuView = lb;
    return lb;
}
  • NaviBarView.m - 一些設置方法
#pragma mark - set

- (void)clearNavBarBackgroundColor {
    _statusBar.backgroundColor = [UIColor clearColor];
    _navigationBar.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor whiteColor];
    [_navigationBar removeChildByTag:kTagLine];
}

- (void)setNavigationTitle:(NSString *)title {
    self.titleLabel.text = title;
}

- (void)setBackImage:(NSString *)imageName {
    UIImage *background = [UIImage imageNamed:imageName];
    UIImage *backgroundOn = [UIImage imageNamed:imageName];
    [_backBtn setImage:background forState:UIControlStateNormal];
    [_backBtn setImage:backgroundOn forState:UIControlStateHighlighted];
}

####### 2.BaseViewController基類的實現(xiàn)

  • BaseViewController.h部分代碼
/**
 基類
 */
@interface BaseViewController : UIViewController

#pragma mark - 導航條相關
@property(nonatomic, copy)NSString *naviTitle;  // 標題
/** 導航條 */
@property(nonatomic, strong)NaviBarView *topNavBar;
/** 內(nèi)容視圖 */
@property (strong, nonatomic) UIView *containerView;

// 返回按鈕點擊操作
- (void)doBackPrev;
// 掃碼和心愿單
- (void)addScanAndWishList;
// 設置導航條透明
- (void)clearNavBarBackgroundColor;

// 添加按鈕
- (UIButton *)addBtnWithTitle:(NSString *)title action:(SEL)action;

- (UILabel *)addRightMenu:(NSString *)actionName withAction:(SEL)action;

@end
  • BaseViewController.m實現(xiàn)
@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // setup data
    self.view.backgroundColor = [UIColor whiteColor];
    // 所有界面隱藏導航欄,用自定義的導航欄代替
    self.fd_prefersNavigationBarHidden = YES;
    // drawUI
    [self drawTopNaviBar];
}
@end

為什么要設置fd_prefersNavigationBarHidden參數(shù)為 YES醉箕,因為本項目是使用FDFullscreenPopGesture來管理導航條钾腺,隱藏系統(tǒng)的導航條已經(jīng)被它所實現(xiàn)了。

- (void)fd_viewWillAppear:(BOOL)animated
{
    // Forward to primary implementation.
    [self fd_viewWillAppear:animated];
    
    if (self.fd_willAppearInjectBlock) {
        self.fd_willAppearInjectBlock(self, animated);
    }
    
    //設置導航的顯示/隱藏
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIViewController *vc = [self.navigationController.viewControllers lastObject];
        if (vc.fd_prefersNavigationBarHidden) {
            [self.navigationController setNavigationBarHidden:YES animated:NO];
        } else {
            [self.navigationController setNavigationBarHidden:NO animated:NO];
        }
    });
}
  • 在界面即將顯示的時候讥裤,將導航條推至最前面放棒,避免被其他視圖遮擋
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.navigationController.navigationBar.height == 0) {
        _topNavBar.alpha = 0;
    }
    // 將導航放到最頂部,不然后面有其它的層會被覆蓋
    [self.view bringSubviewToFront:_topNavBar];
}
  • 導航條的繪制
/// 在旋轉界面時重新構造導航條
- (void)drawTopNaviBar {
    if (_topNavBar) {
        [_topNavBar removeFromSuperview];
    }
    // 添加自定義的導航條
    NaviBarView *naviBar = [[NaviBarView alloc] initWithController:self];
    [self.view addSubview:naviBar];
    self.topNavBar = naviBar;
    
    if (![self isKindOfClass:[HomeViewController class]] && ![self isKindOfClass:[AccountViewController class]]) {
        // 添加返回按鈕
        [_topNavBar addBackBtn];
        // 添加底部分割線 - 如果不需要添加,這里處理即可
        [_topNavBar addBottomSepLine];
    }
    
    // 自動放一個容器在下面,如果全屏的界面就往 self.view 加子,非全屏的往 container 加
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, ScreenHeight - NavHeight)];
    self.containerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.containerView];
}
  • 還有其他代碼請下載本文的項目即可
3.使用
  • 新建的 VC 必須繼承自BaseViewController才可以使用
3.1 導航條添加掃碼,搜索己英,心愿單功能
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加掃碼,搜索,心愿單
    [self addScanAndWishList];
}
image.png
3.2 導航條正常樣式+右側添加分享圖標
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.naviTitle = @"First VC";
    
    [self addRightMenu:@"分享" withAction:@selector(tapShare)];
}
image.png
3.3 導航條只有返回按鈕间螟,沒有標題,沒有底部線條
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.naviTitle = @"隱藏導航條分割線";
    [self clearNavBarBackgroundColor];
}
image.png

更多功能损肛,只需要根據(jù)需要厢破,自定義導航條視圖類NaviBarView即可。


  • 如有錯誤治拿,歡迎指正摩泪,多多點贊,打賞更佳劫谅,您的支持是我寫作的動力见坑。

項目連接地址 - FDFullScreenPopGestureDemo

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市捏检,隨后出現(xiàn)的幾起案子荞驴,更是在濱河造成了極大的恐慌,老刑警劉巖贯城,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熊楼,死亡現(xiàn)場離奇詭異,居然都是意外死亡能犯,警方通過查閱死者的電腦和手機鲫骗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悲雳,“玉大人挎峦,你說我怎么就攤上這事『掀埃” “怎么了坦胶?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我顿苇,道長峭咒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任纪岁,我火速辦了婚禮凑队,結果婚禮上,老公的妹妹穿的比我還像新娘幔翰。我一直安慰自己漩氨,他們只是感情好,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布遗增。 她就那樣靜靜地躺著叫惊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪做修。 梳的紋絲不亂的頭發(fā)上霍狰,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音饰及,去河邊找鬼蔗坯。 笑死,一個胖子當著我的面吹牛燎含,可吹牛的內(nèi)容都是我干的宾濒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼瘫镇,長吁一口氣:“原來是場噩夢啊……” “哼仓坞!你這毒婦竟也來了生真?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤烘嘱,失蹤者是張志新(化名)和其女友劉穎鹦付,沒想到半個月后尚粘,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡敲长,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年郎嫁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片祈噪。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡泽铛,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辑鲤,到底是詐尸還是另有隱情盔腔,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站弛随,受9級特大地震影響瓢喉,放射性物質發(fā)生泄漏。R本人自食惡果不足惜舀透,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一栓票、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧愕够,春花似錦走贪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至强衡,卻和暖如春擦秽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背漩勤。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工感挥, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人越败。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓触幼,卻偏偏與公主長得像,于是被迫代替她去往敵國和親究飞。 傳聞我的和親對象是個殘疾皇子置谦,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容