iOS导航栏最佳实践

见过很多项目,设置不同导航栏,有隐藏与显示设置的,每个页面有画页将要出现,离开画页处理导航栏图片文字颜色的。
工作年限,加班时间不会使你技术进步,只会让你习惯垃圾代码的写法,记住垃圾代码,抄来抄去垃圾代码,不愿在改动垃圾代码,多么可悲的大神啊

用到第三方 FDFullscreenPopGesture,思路,设置一个全局的导航栏背景或者图片,title baritem颜色,不一样的背景title一行隐藏全局的导航栏,在自己页面写个view模拟导航。
实际项目运用,显示与隐藏各种切换未出任何异常。

比如有个CKKShareVC需要特别的导航栏。操作流程
一、当前页面处理


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
}

二、基类导航处理

#pragma  mark - life cycle

+ (void)initialize {

    [[UINavigationBar appearance] setTranslucent:NO];
    NSDictionary *titleAttributeDict = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                         NSFontAttributeName:[UIFont systemFontOfSize:18.0]};
    [[UINavigationBar appearance] setTitleTextAttributes:titleAttributeDict];
    
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Main001"]
                                      forBarPosition:UIBarPositionAny
                                          barMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    // UIBarButtonItem
    UIBarButtonItem *item=[UIBarButtonItem appearance];
    item.tintColor = [UIColor whiteColor];
    
    NSDictionary *itemAttributeDict= @{NSFontAttributeName:[UIFont systemFontOfSize:15],
                                       NSForegroundColorAttributeName : [UIColor whiteColor]};
    [item setTitleTextAttributes:itemAttributeDict forState:UIControlStateNormal];
    
    // backItem 这里设置全局返回按钮,不够灵活,美工给的图不到位的话,很难调整
    //        UIImage *originImage = [UIImage imageNamed:@"navback"];
    //        UIEdgeInsets insets = UIEdgeInsetsMake(0.0f, -10.0f, 0.0f, -10.0f);
    //        UIColor *fillColor = [UIColor clearColor];
    //        UIImage *backButtonImage = [originImage imageByInsetEdge:insets withColor:fillColor];
    //        [item setBackButtonBackgroundImage:[originImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 18, 0, 0) ]
    //                                  forState:UIControlStateNormal
    //                                barMetrics:UIBarMetricsDefault];
    
    
    //    UIOffset minOffset =
    //    UIOffsetMake(NSIntegerMin, NSIntegerMin);
    //    [item setBackButtonTitlePositionAdjustment:minOffset
    //                                 forBarMetrics:UIBarMetricsDefault];
    
}
#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor  whiteColor];
    self.delegate = self;
}
// 实现代理
#pragma mark - Private Methods
#pragma mark -
#pragma mark Whether need Navigation Bar Hidden
- (BOOL) needHiddenBarInViewController:(UIViewController *)viewController {
    
    BOOL needHideNaivgaionBar = NO;
    /// 这里设置CKKShareVC要隐藏状态栏
    if ([viewController isKindOfClass: NSClassFromString(@"CKKShareVC")]) {
        
        needHideNaivgaionBar = YES;
    }
    
    return needHideNaivgaionBar;
}
#pragma mark - UINaivgationController Delegate
#pragma mark -
#pragma mark Will Show ViewController
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 在 NavigationController 的这个代理方法中, 设置导航栏的隐藏和显示
    [self setNavigationBarHidden: [self needHiddenBarInViewController: viewController]
                        animated: animated];
}

你可能感兴趣的:(iOS导航栏最佳实践)