300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 自定义导航栏 系统导航栏一堆bug

自定义导航栏 系统导航栏一堆bug

时间:2023-03-10 14:26:12

相关推荐

自定义导航栏 系统导航栏一堆bug

开发的过程中,我们可能会遇到有的界面需要把导航栏隐藏的问题。而一旦在该界面隐藏了导航栏,在与其他界面交互的过程中就会出现一些很恶心的问题,特别是有的从桌面啊,通知啊,widget进入某些界面的话,问题就更多。因为导航栏属于系统层级,某一界面的修改都将影响所有界面的导航栏显示。而解决这种问题的方式就是将系统的导航栏隐藏,自定义一个导航栏。纵观世面上的APP,很多已经采用了诸如此类的方式,比如淘宝,美团,携程等等。不多说,笔者直接将自己采用的方式告诉大家:

//

// LEOBaseNavigationController.h

// PrivacyGuard

//

// Created by Ternence on 15/11/11.

// Copyright © LEO. All rights reserved.

//

#import <UIKit/UIKit.h>

#import "LEOBaseViewController.h"

@interface LEOBaseNavigationController : UINavigationController

@property (nonatomic,strong) UINavigationBar *navigationBar;

@property (nonatomic,strong) UINavigationItem *titleItem;

- (void)setupNavigationBar;

- (void)backButtonAction;

@end

//

// LEOBaseNavigationController.m

// PrivacyGuard

//

// Created by Ternence on 15/11/11.

// Copyright © LEO. All rights reserved.

//

#import "LEOBaseNavigationController.h"

@interface LEOBaseNavigationController ()

@end

@implementation LEOBaseNavigationController

- (void)viewDidLoad

{

[superviewDidLoad];

}

- (void)setupNavigationBar

{

self.navigationController.navigationBar.hidden =YES;

self.navigationBar = [[UINavigationBaralloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,64)];

self.navigationBar.barTintColor =C5;

[self.viewaddSubview:self.navigationBar];

self.titleItem = [[UINavigationItemalloc] initWithTitle:NSLocalizedString(@"device monitor Bettery Percentage",nil)];

self.navigationBar.titleTextAttributes =@{NSForegroundColorAttributeName:C4, NSFontAttributeName: T1};

self.titleItem.leftBarButtonItem = [[UIBarButtonItemalloc] initWithImage:[[UIImageimageNamed:@"navigationBar_leftBack_blue"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

style:UIBarButtonItemStylePlain

target:self

action:@selector(backButtonAction)];

self.navigationBar.items =@[self.titleItem];

}

- (void)backButtonAction

{

[self.navigationControllerpopViewControllerAnimated:YES];

}

@end

你可能会遇到以下问题,我就帮人帮到底,送佛送到西,一并帮你解决了,不谢!

/**

*关于导航栏

1.如需自定义导航栏,控制器继承自LEOBaseNavigationController,,主要处理导航栏事宜。父类控制器不会主动调用setupNavigationBar方法。子类重载setupNavigationBar方法,调用super方法后,默认会有一个白色的左边有返回按钮的导航栏。

2.如需更改导航栏颜色,请在子类方法中添加代码self.navigationBar.barTintColor = [UIColor greenColor];相应的title颜色如需更改,添加代码self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor]};

3.如需添加导航栏底部的一根线,添加代码

UIView *dividerLine = [[UIView alloc] initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, 1)];

dividerLine.backgroundColor = HHColorWithAlpha(0, 0, 0, 0.2);

[self.navigationBar addSubview:dividerLine];

4.如果想要添加导航栏右边的按钮,添加代码

self.titleItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Information-Icon"] style:UIBarButtonItemStyleBordered target:selfaction:@selector(vidgetGuideBtnDidClicked)];相应的点击事件自行处理

5.如果想要在返回按钮的点击事件中处理一些事物,重载backButtonAction方法,最后调用super方法

6.如果要动态改变左右按钮的图片或文字或点击事件,示例如下:

self.titleItem.leftBarButtonItem.image = [UIImage imageNamed:@"Information-Icon"];

self.titleItem.leftBarButtonItem.title = @"fuck";

self.titleItem.leftBarButtonItem.action = @selector(backButtonAction);

7.如果遇到图片被自动渲染成其他颜色,请尝试如下修改

UIImage *image = [UIImage imageNamed:@"Information-Icon"];

self.titleItem.leftBarButtonItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

8.Controller内部的view frame从64开始,因为导航栏属于控制器。

9.如需更改title文字,请添加代码

self.titleItem.title = NSLocalizedString(@"device monitor Bettery Percentage", nil);

或self.titleItem.titleView;

*/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。