現(xiàn)在通用的不同樣式導航欄切換實現(xiàn)方式如下兩種
今日頭條的實現(xiàn):
image
今日頭條的實現(xiàn)方式比較簡單:
在第二個頁面隱藏self.navaigationController.navigationbar,然后添加一個自己新建的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;可以自動控制self.navigationcontroller.navigationbar的顯示和隱藏
微信的實現(xiàn):
image
微信的實現(xiàn)方式其實更加優(yōu)雅美觀膏燕,左右切換時還可以看到導航欄上面item的淡入淡出切換效果峻堰,其實這就是系統(tǒng)級別自帶的動效上枕。
猜測實現(xiàn)方式如下:
- 將self.navigationController.navigatonBar子視圖中的背景層設(shè)置為透明
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
- 為self.view添加一個跟navigationbar同樣大小的UIview作為新的背景層,不同的頁面自定義不同的顏色妨马。這里采用基類實現(xiàn)
#import <UIKit/UIKit.h>
@interface PCBaseViewController : UIViewController
@property (nonatomic, strong) UIColor *topBarBackgroundColor;
@property (nonatomic, strong) UIView *navigationBarSeperateView; //導航欄底部的分割線
@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è)置自定義背景層的顏色及分割線的顯示隱藏挺举。
另一中實現(xiàn)方式是通過分類,hook到viewcontroller的viewwillAppear方法烘跺,并在里面添加自定義視圖等湘纵。但有個地方需要注意的是鍵盤彈出,系統(tǒng)提示彈出時對應(yīng)的UIInputViewController滤淳、UIAlertController也會讓viewwillAppear方法響應(yīng)需要區(qū)分掉梧喷。