美文网首页iOS
iOS 导航栏从一个有颜色返回无颜色解决闪烁

iOS 导航栏从一个有颜色返回无颜色解决闪烁

作者: iOSTNP | 来源:发表于2017-04-10 11:37 被阅读41次

之前在群里经常看到有遇到类似的问题提问,听他们说在网上也没有搜集到答案,那么下面我来给大家提供两种解决方案:

1.直接上代码:

AppDelegate  里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

//NavigationViewController自定义的

self.window.rootViewController = [[NavigationViewController alloc]initWithRootViewController:[ViewController new]];

[self.window makeKeyAndVisible];

return YES;

}

第一个ViewController里面的代码

#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()

{

UIView *_navView;

}

@end

#define kMainWidth [UIScreen mainScreen].bounds.size.width

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:YES animated:animated];

}

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor greenColor];

UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];

addBtn.frame = CGRectMake(0, 200, 120, 40);

[addBtn setTitle:@"下一个页面" forState:(UIControlStateNormal)];

[addBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

[addBtn addTarget:self action:@selector(addClick:) forControlEvents:(UIControlEventTouchUpInside)];

addBtn.backgroundColor = [UIColor redColor];

[self.view addSubview:addBtn];

[self setupNavbarView];

}

-(void)addClick:(UIButton *)sender

{

SecondViewController *VC = [SecondViewController new];

[self .navigationController pushViewController:VC animated:YES];

}

- (void)setupNavbarView

{

_navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainWidth, 64)];

_navView.backgroundColor=[UIColor clearColor];

[self.view addSubview:_navView];

UILabel *titles = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kMainWidth, 64)];

titles.text = @"首页";

titles.textAlignment = NSTextAlignmentCenter;

titles.font = [UIFont systemFontOfSize:18];

titles.textColor = [UIColor blackColor];

[_navView addSubview:titles];

}

SecondViewController里面的代码

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:NO animated:animated];

}

- (void)viewDidLoad

{

[super viewDidLoad];

self.title = @"二级页面";

self.view.backgroundColor = [UIColor grayColor];

}

就这么简单了

我在贴上自定NavigationViewController的代码继承于UINavigationController

#import "NavigationViewController.h"@interface NavigationViewController ()@end

@implementation NavigationViewController

+ (void)initialize

{

[self setupNavBar];

}

+ (void)setupNavBar

{

UINavigationBar *navBar = [UINavigationBar appearance];

[navBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

[navBar setShadowImage:[[UIImage alloc] init]];

navBar.barTintColor = [UIColor redColor];

navBar.alpha = 0.96;

if ([[UIDevice currentDevice].systemVersion integerValue] >=8.0 && [UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)])

{

[navBar setTranslucent:NO];

}

navBar.tintColor=[UIColor darkGrayColor];

NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];

textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];

textAttrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:17];

[navBar setTitleTextAttributes:textAttrs];

}

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

{

if (self.viewControllers.count > 0) {

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_return_black"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];

viewController.navigationItem.leftBarButtonItem = backItem;

}

[super pushViewController:viewController animated:animated];

}

- (void)back

{

[self popViewControllerAnimated:YES];

}

- (void)viewDidLoad {

[super viewDidLoad];

}

如果觉得从第一个Viewcontroller  push到第二个都需要在viewWillAppear
里面添加[self.navigationController setNavigationBarHidden:NO animated:animated];很是麻烦

那么你也可以写一个Viewcontroller让所有要push的页面都继承与这个页面就好 就不用重复写

其实有时候在第一个页面实现

-(void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

[self.navigationController setNavigationBarHidden:NO animated:animated];

}

也是可以的  但是有时候却不可以

我在项目中这样的确可以解决从一个有颜色的导航到一个没有颜色的导航 或者从一个没有颜色到有颜色的导航 不会出现任何闪烁或者黑导航的状况,第一次写的不好 希望能帮助大家。

相关文章

网友评论

    本文标题:iOS 导航栏从一个有颜色返回无颜色解决闪烁

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