美文网首页
iOS 自定义UINavigationController

iOS 自定义UINavigationController

作者: iosder | 来源:发表于2018-01-26 12:08 被阅读184次

    自定义navbar

    实习中遇到了一个问题,使用系统自带UINavigationController的时候,有些效果做不到。比如各个界面的navbar需要不同的颜色,pop的时候颜色切换很不自然

    push后.png pop时.png

    这样的效果很不美观,而很多知名APP的切换是非常自然的,于是便去网上搜索解决办法

    代码部分

    自定义一个继承于UINavigationController的子类,添加如下方法(此方法可放在load方法里)

    + (void)setUpBase
    {
        UINavigationBar *bar = [UINavigationBar appearance];
        bar.barTintColor = [UIColor redColor];
        [bar setTintColor:[UIColor darkGrayColor]];
        bar.translucent = YES;
        [bar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [bar setShadowImage:[UIImage new]];
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        // 设置导航栏字体颜色
        UIColor * naiColor = SYJColor(100, 100, 100, 1);
        attributes[NSForegroundColorAttributeName] = naiColor;
        attributes[NSFontAttributeName] = [UIFont systemFontOfSize:18];;
        bar.titleTextAttributes = attributes;
    }
    

    重写pushViewController方法

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        if (self.childViewControllers.count > 0) {
            viewController.hidesBottomBarWhenPushed = YES;
            // 就有滑动返回功能
            self.interactivePopGestureRecognizer.delegate = nil;
            //返回按钮自定义
            UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer.width = -15;
            
            UIButton *button = [[UIButton alloc] init];
            [button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateHighlighted];
            button.frame = CGRectMake(0, 0, 33, 33);
            
            if (@available(ios 11.0,*)) {
                button.contentEdgeInsets = UIEdgeInsetsMake(0, -15,0, 0);
                button.imageEdgeInsets = UIEdgeInsetsMake(0, -10,0, 0);
            }
            
            [button addTarget:self action:@selector(backButtonTapClick) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
            
            viewController.navigationItem.leftBarButtonItems = @[negativeSpacer, backButton];
        }
        [super pushViewController:viewController animated:animated];
    }
    #pragma mark - 点击
    - (void)backButtonTapClick {
        
        [self popViewControllerAnimated:YES];
    }
    

    然后在你各个UIViewController中添加一个view

        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, NAVBAR_HEIGHT+STATUS_HEIGHT)];//NAVBAR_HEIGHT+STATUS_HEIGHT指navbar的高度和status的高度
        view.backgroundColor=[UIColor redColor];//加上你要的颜色
        [self.view addSubview:view];
    

    效果

    push后.png
    pop时.png

    相关文章

      网友评论

          本文标题:iOS 自定义UINavigationController

          本文链接:https://www.haomeiwen.com/subject/whiiaxtx.html