微信與今日頭條導(dǎo)航欄切換實(shí)現(xiàn)

現(xiàn)在通用的不同樣式導(dǎo)航欄切換實(shí)現(xiàn)方式如下兩種

今日頭條的實(shí)現(xiàn):

image

今日頭條的實(shí)現(xiàn)方式比較簡單:
在第二個(gè)頁面隱藏self.navaigationController.navigationbar,然后添加一個(gè)自己新建的UINavigationbar。

主要代碼如下:
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    // 新建navigationbar并將self.navigationItem push到堆棧中去。
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
或者如果集成了FDFullscreenPopGesture直接設(shè)置self.fd_prefersNavigationBarHidden = YES;可以自動(dòng)控制self.navigationcontroller.navigationbar的顯示和隱藏

微信的實(shí)現(xiàn):

image

微信的實(shí)現(xiàn)方式其實(shí)更加優(yōu)雅美觀,左右切換時(shí)還可以看到導(dǎo)航欄上面item的淡入淡出切換效果,其實(shí)這就是系統(tǒng)級(jí)別自帶的動(dòng)效。
猜測實(shí)現(xiàn)方式如下:

  • 將self.navigationController.navigatonBar子視圖中的背景層設(shè)置為透明
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
  • 為self.view添加一個(gè)跟navigationbar同樣大小的UIview作為新的背景層,不同的頁面自定義不同的顏色。這里采用基類實(shí)現(xiàn)
#import <UIKit/UIKit.h>

@interface PCBaseViewController : UIViewController
@property (nonatomic, strong) UIColor *topBarBackgroundColor;
@property (nonatomic, strong) UIView *navigationBarSeperateView;                //導(dǎo)航欄底部的分割線
@end

#import "PCBaseViewController.h"
#import "UIColor+HEX.h"

@interface PCBaseViewController ()
@property (nonatomic, strong) UIView *topBarView;
@end

@implementation PCBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
    self.topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    self.topBarView.layer.masksToBounds = YES;
    self.topBarView.backgroundColor = self.topBarBackgroundColor ? self.topBarBackgroundColor : [UIColor whiteColor];
    self.navigationBarSeperateView.frame = CGRectMake(0, 63.5, ScreenWidth, 1);
    self.navigationBarSeperateView.backgroundColor = [UIColor colorWithHexString:@"e3e3e3"];
    [self.topBarView addSubview:self.navigationBarSeperateView];
    [self.view addSubview:self.topBarView];
    
}

- (UIView *)topBarView {
    if (!_topBarView) {
        _topBarView = [UIView new];
    }
    return _topBarView;
}

- (UIView *)navigationBarSeperateView {
    if (!_navigationBarSeperateView) {
        _navigationBarSeperateView = [UIView new];
    }
    return _navigationBarSeperateView;
}

- (void)setTopBarBackgroundColor:(UIColor *)topBarBackgroundColor {
    _topBarBackgroundColor = topBarBackgroundColor;
    [self.topBarView setBackgroundColor:topBarBackgroundColor];
}

@end
  • 所有的viewcontroller繼承自BaseViewcontroller然后在viewdidiload中設(shè)置自定義背景層的顏色及分割線的顯示隱藏。

另一中實(shí)現(xiàn)方式是通過分類,hook到viewcontroller的viewwillAppear方法,并在里面添加自定義視圖等。但有個(gè)地方需要注意的是鍵盤彈出,系統(tǒng)提示彈出時(shí)對(duì)應(yīng)的UIInputViewController、UIAlertController也會(huì)讓viewwillAppear方法響應(yīng)需要區(qū)分掉。

附上Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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