iOS 11 UIBarButtonItem图片变形 解决方案

只需要写一个 UIBarButtonItem类的扩展 其实原理很简单

扩展类创建就不在描述了 下面只贴上方法的使用

扩展类.h

+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action andFrame:(CGRect)rect;



扩展类.m 

+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action andFrame:(CGRect)rect

{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

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

[btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];

btn.frame = rect;

[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

UIView *containVew = [[UIView alloc] initWithFrame:btn.bounds];

[containVew addSubview:btn];

return [[UIBarButtonItem alloc]initWithCustomView:containVew];

}


其实原理就是将这个Button 放在一个View上  再把这个View 放在UIBarButtonItem上,

而以前则是直接将Button放在Item上  会出现一些图片变形问题。


使用

单一左,或者右

UIBarButtonItem *item1 = [UIBarButtonItem itemWithNorImage:@"123.png" highImage:nil target:self action:@selector(gogo) andFrame:CGRectMake(0,0,40,40)];

[self.navigationItem setLeftBarButtonItem:item1];


多个item

NSArray *arr = [item1,item2];

[self.navigationItem setLeftBarButtonItems:arr];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 4,330评论 0 0
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 7,606评论 2 50
  • 自己用过,写过的一点分类,记录一下先。 NSdate .h中: // 是否为今天 - (BOOL)isToday;...
    水煮杰尼龟阅读 3,523评论 0 0
  • 地点:丹阳 不同于有雄厚政治科技优势的超级城市北上广深,丹阳曾经以传统农业为优势,现在眼镜制造业发展速度,也在推进...
    1卷2阅读 1,684评论 0 0
  • 爱情总有期限我会爱你 一瓶香水的时间香水的味道在四周蔓延如同你的气息 围绕身边 我将香水 放在我的枕边我会爱你 一...
    一枚荔枝阅读 1,756评论 2 5

友情链接更多精彩内容