首页 > 代码库 > 导航条的自定义:背景颜色设置,按钮标题图片设置,图片坐标修改
导航条的自定义:背景颜色设置,按钮标题图片设置,图片坐标修改
一、修改系统原生导航条
修改导航条背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#2295f2"];
自定义导航条按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn-menu-h"] style:UIBarButtonItemStylePlain target:self action:@selector(menuBtnClick)];
自定义的按钮图片距屏幕边缘太远,可通过以下代码修改
修改后的rightBarButtonItem边距
系统默认的rightBarButtonItem边距
self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0,-20,0,0); self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(0,-10,0,10);
设置了导航条背景颜色,会导致按钮标题颜色改变,通过以下方法修改
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
修改标题颜色和字体大小
self.navigationController.navigationBar.titleTextAttributes =@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#ffffff"],NSFontAttributeName:[UIFont systemFontOfSize:15]};
修改按钮标题颜色大小
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#ffffff"],NSFontAttributeName:navItermFont} forState:UIControlStateNormal];
因为导航条是半透明的,如果不做处理导航条的颜色总是和设计颜色有误差
这行代码可以关闭半透明效果,但是会导致坐标0点移动。
[UINavigationBar appearance].translucent = NO;
关闭坐标0点移动
self.edgesForExtendedLayout = UIRectEdgeNone;
关于translucent属性的详细解答请移步:IOS7 导航栏适配--translucent属性设置的问题
二、自定义导航条
如果系统导航条不能满足设计要求,就需要根据设计自定义,代码中只写了标题和左右按钮,根据设计可以加入任意控件.
#import @interface CustomNav : UIView @property(nonatomic,strong)UIButton *buttonLeft; @property(nonatomic,strong)UIButton *buttonRight; - (id)initWithTitle:(NSString *)title; @end #import "CustomNav.h" @implementation CustomNav - (id)initWithTitle:(NSString *)title { self=[super init]; if (self) { self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 64); ##标题 UILabel *titleLabel = [[UILabel alloc]init]; titleLabel.frame = CGRectMake(SCREEN_WIDTH/4.0, 22, SCREEN_WIDTH/2.0, 40); titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor = [UIColor colorWithHexString:@"#ffffff"]; titleLabel.font = [UIFont systemFontOfSize:17]; titleLabel.text = title; [self addSubview:titleLabel]; ##左按钮 self.buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom]; self.buttonLeft.frame = CGRectMake(0, 22, 40, 40); [self addSubview:self.buttonLeft]; self.buttonLeft.titleLabel.font = [UIFont systemFontOfSize:15]; self.buttonLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; ##右按钮 self.buttonRight = [UIButton buttonWithType:UIButtonTypeCustom]; self.buttonRight.frame = CGRectMake(SCREEN_WIDTH - 40, 22, 40, 40); [self addSubview:self.buttonRight]; self.buttonRight.titleLabel.font = [UIFont systemFontOfSize:15]; self.buttonRight.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; self.backgroundColor = [UIColor colorWithHexString:@"#2295f2"]; } return self; } @end
导航条的自定义:背景颜色设置,按钮标题图片设置,图片坐标修改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。