美文网首页
NavigationBar和StatusBar的风格设置

NavigationBar和StatusBar的风格设置

作者: brownfeng | 来源:发表于2016-10-29 10:29 被阅读42次

    navigationBar和statusBar

    我们都知道一个navigaitonController中的navigationBar是统一的,同时对于statusBar的样式也是,如果需要设置统一风格,最好在navigationController中统一设置.

    #import "WBFaceBasicNavigationController.h"
    
    @interface WBFaceBasicNavigationController ()
    
    @end
    
    @implementation WBFaceBasicNavigationController
    
    +(void)initialize{
    // 在这里设置所有关于navigationBar的统一的属性
        UINavigationBar * navigationBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
        [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        navigationBar.translucent = YES;
        
        [navigationBar setShadowImage:[UIImage new]];
        [navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:17]}];
        navigationBar.barTintColor = [UIColor whiteColor];
    }
    
    -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
        viewController.navigationItem.hidesBackButton = YES;
        [super pushViewController:viewController animated:animated];
    }
    
    /**
     *  默认返回值为nil。
     *  当我们调用setNeedsStatusBarAppearanceUpdate时,系统会调用application.window的rootViewController的preferredStatusBarStyle
     *  方法
     *  我们的程序里一般都是用UINavigationController做root,如果是这种情况,那我们自己的UIViewController里的preferredStatusBarStyle根本不会被调用
     *  这个时候不要调用我自己(就是UINavigationController)的preferredStatusBarStyle方法,而是去调用navigationController.topViewController的preferredStatusBarStyle方法
     *
     *  @return stack最上层的vc,我们知道navigation是以stack的形式跳转的
     */
    - (UIViewController *)childViewControllerForStatusBarStyle {
        return self.topViewController;
    }
    
    @end
    

    leftBarbuttonItem和rightBarbuttonItem

    +(instancetype)itemWithImage:(NSString *)image selectedImage:(NSString *)selectedImage target:(id)target action:(SEL)action{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        button.bounds = CGRectMake(0, 0, 30, 30);
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        return [[self alloc] initWithCustomView:button];
    }
    @end
    
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"backbutton.png" selectedImage:@"backbutton.png" target:self action:@selector(faceVerifyComeBack)];
    

    相关文章

      网友评论

          本文标题:NavigationBar和StatusBar的风格设置

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