iOS11下,leftBarButtonItem异常情况
一般leftBarButtonItem加载网络图片或因为其他问题,必须要在leftBarButtonItem使用自定义的UIButton时,就会因为图片太大,导致设置为UIButton的背景图片时,因为图片太大,拉宽leftBarButtonItem。如下图:
原始的方法为xib文件或者是自定义的:
UIButton *leftCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[leftCustomButton setBackgroundImage:[UIImage imageNamed:@"defaultImage"] forState:UIControlStateNormal];
UIBarButtonItem * leftButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftCustomButton];
self.navigationItem.leftBarButtonItems = @[self.headerIconItem];
但是直接设置UIButton的frame在iOS 11下是没有作用的,解决方法如下:
UIButton *leftCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[leftCustomButton setBackgroundImage:[UIImage imageNamed:@"defaultImage"] forState:UIControlStateNormal];
UIView *leftCustomView = [[UIView alloc] initWithFrame: leftCustomButton.frame];
[leftCustomView addSubview: leftCustomButton];
UIBarButtonItem * leftButtonItem =[[UIBarButtonItem alloc] initWithCustomView: leftCustomView];
self.navigationItem.leftBarButtonItems = @[self.headerIconItem];
即在UIButton上在包装一层UIView,即可实现修改leftBarButtonItem的大小。
修改之后如下图: