美文网首页
iOS11导航栏自定义按钮偏移问题

iOS11导航栏自定义按钮偏移问题

作者: 真爱要有你才完美 | 来源:发表于2017-11-03 10:08 被阅读129次

iOS11之前小伙伴通过设置UIBarButtonSystemItemFixedSpace width=-10来解决按钮位置偏移问题, UIButtonleftBtn = [[UIButtonalloc] initWithFrame:CGRectMake(0,0, 44,44)]; 

leftBtn.backgroundColor = [UIColorcyanColor]; [leftBtn setImage:[UIImageimageNamed:@"back.png"]forState:UIControlStateNormal]; 

[leftBtn addTarget:selfaction:@selector(back:)forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItemleftItem = [[UIBarButtonItemalloc] initWithCustomView:leftBtn];

UIBarButtonItem *ItemSpace = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

            if        (MODEL_VERSION >=7.0) {   

                ItemSpace.width= -10;

                        }

                    self.navigationItem.leftBarButtonItems=@[ItemSpace, leftItem];

如今iOS11上不可用了尴尬!!!

后来通过改变按钮的 contentEdgeInsets和imageEdgeInsets的值成功改变了按钮的偏移问题,单独设置contentEdgeInsets也可达到一定的效果。

 UIButtonleftBtn = [[UIButtonalloc] initWithFrame:CGRectMake(0,0, 44,44)]; leftBtn.backgroundColor = [UIColorcyanColor]; 

leftBtn.contentEdgeInsets =UIEdgeInsetsMake(0, -20,0, 0); 

leftBtn.imageEdgeInsets =UIEdgeInsetsMake(0, -15,0, 0); 

[leftBtn setImage:[UIImageimageNamed:@"back"]forState:UIControlStateNormal]; 

[leftBtn  addTarget:selfaction:@selector(back:)forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItemleftItem = [[UIBarButtonItemalloc] initWithCustomView:leftBtn];

self.navigationItem.leftBarButtonItems=@[leftItem];

相关文章

网友评论

      本文标题:iOS11导航栏自定义按钮偏移问题

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