iOS導航欄自定義返回按鈕和設置導航欄透明交互

在我的項目里經常會遇到自定義導航欄的返回按鈕,以及設置導航欄的背景為透明。
但是這樣子設置會遇到兩個問題:
1.如果設置不當,返回的手勢交互會不能用。
2.導航欄透明的狀態和不透明狀態切換很麻煩,而且效果不好。
在這里我分享一種解決辦法,供大家參考:
首先你要創建一個BaseViewController,這樣你的一些基礎的設置都可以在這個Controller里面進行了。
BaseViewController的h文件

@interface BaseViewController : UIViewController<UIGestureRecognizerDelegate>
//返回按鈕的pop
-(void)viewWillBack;
//這個作為透明導航欄下面的顏色設置view;
@property (nonatomic, strong) UIView *NavBarView;

@end

BaseViewController的m文件

- (void)viewDidLoad
{
    [super viewDidLoad];
//開啟交互手勢,我一般是在自定義的導航欄里面設置的,UIGestureRecognizerDelegate
self.navigationController.interactivePopGestureRecognizer.delegate = self;
//設置導航欄為透明顏色
[self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
 //創建底部的導航欄試圖
    [self bulidNavBarView];
//第一個控制器不需要加返回按鈕
    if ([[self.navigationController viewControllers] count] > 1) {
        [self resetBackBarButton];
    }
}

-(void)viewWillBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

//設置返回按鈕
- (void)resetBackBarButton
{
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBarButton.frame = CGRectMake(0, 0, 40, 40);
    [leftBarButton setImage:[[UIImage imageNamed:@"nav_back"] imageWithOverlayColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    
    [leftBarButton addTarget:self action:@selector(viewWillBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
//用于調整返回按鈕的位置
    UIBarButtonItem *space_item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space_item.width = -10;
//通過這個方法設置,手勢返回的操作就不會關閉了
self.navigationItem.leftBarButtonItems = @[space_item, item];    
}

接下來就是在BaseViewController的頂部加上一個NavBarView

//當然如果你要加圖片可以換成UIImageView
- (void)bulidNavBarView{
    //如果這里導航欄設置成半透明就是0,如果不是半透明就是-64,可以根據實際情況調整,就是要放在view頂部
    self.navBarView = [[UIView alloc]initWithFrame:CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, 64)];
    //設置顏色為白色;
    self.navBarView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.navBarBgView];
    
}
控制導航底部的陰影線條
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隱藏
    self.navigationController.navigationBar.shadowImage = [UIImage new];
//如果現實的話換一張陰影的圖片就可以了
}

當然有可能還需要設置導航欄的其他屬性,所以我喜歡創建一個MainNavigationController,這個里面來統一設置

//設置導航欄標題的顏色和字體
 NSDictionary * dict = @{
                            NSFontAttributeName:[UIFont systemFontOfSize:17],
                            NSForegroundColorAttributeName:color
                            };
    self.navigationBar.titleTextAttributes = dict;

運用導航欄的代理方法UINavigationControllerDelegate來進行push之后隱藏Tabbar的操作

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

    viewController.hidesBottomBarWhenPushed = YES;
    
    [super pushViewController:viewController animated:animated];
    viewController.hidesBottomBarWhenPushed = NO;

}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    
    UIViewController *viewCol = [super popViewControllerAnimated:animated];
    
    if (self.viewControllers.count == 1) {
        
        viewCol.tabBarController.tabBar.hidden = NO;
    }else{
        
        viewCol.tabBarController.tabBar.hidden = YES;
    }
    
    return viewCol;
}

可能上面并不是最好的解決方案,如果你們有什么好的建議可以互相學習,不足之處還請見諒。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容