关于自定义导航栏返回按钮

1. 普通的自定义导航按钮

1> 只有返回图标的

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];

leftBtn.frame=CGRectMake(0,0,12,18);

[leftBtnsetBackgroundImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal];

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked)forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithCustomView:leftBtn];

2> 有图标 有文字

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];

leftBtn.frame=CGRectMake(0,0,90,25);

[leftBtnsetImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal];

[leftBtnsetTitle:@"添加好友"forState:UIControlStateNormal];

[leftBtnsetTitleColor:[UIColorcolorWithRed:22/255.0green:88/255.0blue:224/255.0alpha:1]forState:UIControlStateNormal];

leftBtn.titleLabel.font= [UIFontsystemFontOfSize:15];

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked)forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithCustomView:leftBtn];

3> 有的时候图标和文字之间的距离会显的太近 ,或者这个自定义的返回bar会距离左边太近

对于图标和文字距离太近,其实很简单 : [leftBtnsetTitle:@"   添加好友"forState:UIControlStateNormal];

在文字之前加几个空格 ,看你满意喽 

对于整个返回按钮距离左边太近 ,我们可以这么写

//自定义导航左侧按钮

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];

leftBtn.frame=CGRectMake(0,0,90,25);

[leftBtnsetImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal];

[leftBtnsetTitle:@"添加好友"forState:UIControlStateNormal];

[leftBtnsetTitleColor:[UIColorcolorWithRed:22/255.0green:88/255.0blue:224/255.0alpha:1]forState:UIControlStateNormal];

leftBtn.titleLabel.font= [UIFontsystemFontOfSize:15];

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem*leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:leftBtn];

//解决按钮不靠左的问题.

UIBarButtonItem*nagetiveSpacer = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil];

nagetiveSpacer.width= -15;//这个值可以根据自己需要自己调整

self.navigationItem.leftBarButtonItems=@[nagetiveSpacer, leftBarButtonItem];


就这么多 over。

你可能感兴趣的:(关于自定义导航栏返回按钮)