1、iOS11以下設(shè)置navigationItem的邊距
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
backView.backgroundColor = [UIColor magentaColor];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
// 用于控制間距的item
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
/**
* width為負(fù)數(shù)時(shí)相當(dāng)于向左移動(dòng)width數(shù)值的距離,為正數(shù)時(shí)相當(dāng)于向右移動(dòng)width數(shù)值的距離
* 自己測(cè)了下,左邊應(yīng)該是16px,右邊應(yīng)該是12px
*/
spaceItem.width = -15;
self.navigationItem.leftBarButtonItems = @[spaceItem,backItem];
2、iOS設(shè)置navigationItem的邊距
- (void)viewDidLoad {
[super viewDidLoad];
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 44, 44);
[backButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:backButton];
if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
} else {
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = -15;
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
self.navigationItem.leftBarButtonItems = @[spaceItem,backItem];
}
}
- (void)viewDidLayoutSubviews{
if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
UINavigationItem *item = self.navigationItem;
NSArray *array = item.leftBarButtonItems;
if (array && array.count) {
// 注意:這里設(shè)置的第一個(gè)leftBarButtonItem的customeView不能為空,也就是不要設(shè)置UIBarButtonSystemItemFixedSpace這種風(fēng)格的item
UIBarButtonItem *btnItem = array[0];
UIView *view = [[btnItem.customView.superview superview] superview];
NSArray *arrayConstraint = view.constraints;
for (NSLayoutConstraint *constant in arrayConstraint) {
CGFloat space = fabs(constant.constant);
// 非plus手機(jī)為16 plus手機(jī)為20
if (space == 16 || space == 20) {
constant.constant = 0;
}
}
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。