美文网首页
iOS 11和iOS10以下自定义返回按钮的问题

iOS 11和iOS10以下自定义返回按钮的问题

作者: i_MT | 来源:发表于2017-11-08 14:57 被阅读560次

在项目开发的过程中,每当苹果出新的系统版本就是我们最忙的时候,到处测试界面的问题,烦!!!

最近总是觉得自己自定义的返回按钮向右偏移20的问题,解决方法如下:

if(@available(iOS11.0, *)) {

//iOS11.0以上

UIButton*leftBtn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,20,40)];

//leftBtn.backgroundColor = [UIColor blueColor];

leftBtn.contentEdgeInsets=UIEdgeInsetsMake(0, -10,0,0);//根据自己的要求设置

leftBtn.imageEdgeInsets=UIEdgeInsetsMake(0,-8,0,0);//根据自己的要求设置

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

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

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

self.navigationItem.leftBarButtonItems=@[leftItem];

}else{

//自定义左边的按钮

UIButton*leftButton = [[UIButtonalloc]init];

leftButton.frame=CGRectMake(0,0,20,20);

[leftButtonsetBackgroundImage:[UIImageimageNamed:@"title_btn_back"]forState:UIControlStateNormal];

[leftButtonaddTarget:selfaction:@selector(backShowSchoolListVc)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem*leftBarButtonItems = [[UIBarButtonItemalloc]initWithCustomView:leftButton];

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

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

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

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

}

相关文章

网友评论

      本文标题:iOS 11和iOS10以下自定义返回按钮的问题

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