iOS 11之前自定義導(dǎo)航欄的左按鈕代碼如下
// UIButton *toRootVC = [UIButton buttonWithType:UIButtonTypeCustom];
// [toRootVC setImage:[UIImage imageNamed:@"首頁(yè)_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];
//設(shè)置一個(gè)寬度為負(fù)的item與按鈕一同加入
// leftHelpItem.width = -15;
// self.navigationItem.leftBarButtonItems = @[leftHelpItem ,[[UIBarButtonItem alloc] initWithCustomView:toRootVC]];
但是在iOS 11中這樣的設(shè)置無(wú)效了
經(jīng)過(guò)一番折騰:修改代碼如下
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:@"首頁(yè)_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];
以上代碼解決了按鈕顯示上的問(wèn)題穿撮,但是仍存在響應(yīng)范圍較以前版本偏小的問(wèn)題,歡迎交流和指正媳危。