// 初始化一个返回按钮
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
// button.backgroundColor = [UIColor greenColor];
// 为返回按钮设置图片样式
[button setImage:[UIImage imageNamed:@"goback"] forState:UIControlStateNormal];
// 设置返回按钮触发的事件
[button addTarget:self action:@selector(backBarButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// 初始化一个BarButtonItem,并将其设置为返回的按钮的样式
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
if(@available(iOS 11.0, *)) {
[[button.heightAnchor constraintEqualToConstant:40] setActive:YES];//关键地方
[[button.widthAnchor constraintEqualToConstant:40] setActive:YES];//关键地方
}
// 将BarButtonItem添加到LeftBarButtonItem上
self.navigationItem.leftBarButtonItem = backButton;