iOS 11之前自定义导航栏的左按钮代码如下
// UIButton *toRootVC = [UIButton buttonWithType:UIButtonTypeCustom];
// [toRootVC setImage:[UIImage imageNamed:@"首页_u82"] forState:UIControlStateNormal];
// [toRootVC setFrame:CGRectMake(0, 0, 50, 50)];
// [toRootVC addTarget:self action:@selector(toRootVCButton:) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *leftHelpItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
//设置一个宽度为负的item与按钮一同加入
// leftHelpItem.width = -15;
// self.navigationItem.leftBarButtonItems = @[leftHelpItem ,[[UIBarButtonItem alloc] initWithCustomView:toRootVC]];
但是在iOS 11中这样的设置无效了
经过一番折腾:修改代码如下
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
UIButton *leftbtn =[UIButton buttonWithType:UIButtonTypeCustom];
leftbtn.frame = CGRectMake(-15, -5, 55, 55);
[leftbtn setImage:[UIImage imageNamed:@"首页_u82"] forState:UIControlStateNormal];
[leftbtn addTarget:self action:@selector(toRootVCButton:) forControlEvents:UIControlEventTouchUpInside];
[leftView addSubview:leftbtn];
[self.view addSubview:leftView];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftView];
以上代码解决了按钮显示上的问题,但是仍存在响应范围较以前版本偏小的问题,欢迎交流和指正。
网友评论