ios怎么设置导航条左边的按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios怎么设置导航条左边的按钮相关的知识,希望对你有一定的参考价值。

见代码
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(0, 0, 44, 44);

btn.adjustsImageWhenHighlighted = NO;

[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

[btn addTarget:self action:Action forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

negativeSpacer.width = -12;//ios7以后右边距默认值18px,负数相当于右移,正数左移

self.navigationItem.leftBarButtonItems=@[negativeSpacer,item];
参考技术A frame 定义是加上 name ,然后连接时 target=name(你要用的frame名,加双引号)

导航条的自定义:背景颜色设置,按钮标题图片设置,图片坐标修改

一、修改系统原生导航条

修改导航条背景颜色

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

 

以上是关于ios怎么设置导航条左边的按钮的主要内容,如果未能解决你的问题,请参考以下文章

react native 怎么隐藏导航栏

ios 导航栏怎么添加左右按钮

苹果手机怎样转发短信

导航条的分割线用CSS怎么设置从中间开始显示

Android如何隐藏底部虚拟按键

新手:Bootstrap 左边栏怎么控制右侧内容部分 不用每个页面都做左边栏或者导航栏吧