修改navigationbar右侧按钮离屏幕边缘位置

先上代码

UIButton *settingBtn = [Utils creatCustomButtonWithFrame:CGRectMake(0, 0, 60, 40) btnTitle:@"设置" btnFontSize:14 titleColor:[UIColor blackColor]];
[settingBtn addTarget:self action:@selector(clickTheSettingBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:settingBtn];
self.navigationItem.rightBarButtonItem = rightBar;

效果为

若想调节该按钮位置,设置

settingBtn.titleLabel.textAlignment = NSTextAlignmentRight;

这样是没效果的。应利用UIBarButtonSystemItemFixedSpace格式,rightbar设为数组,进行填充。

UIButton *settingBtn = [SHUtils creatCustomButtonWithFrame:CGRectMake(0, 0, 60, 40) btnTitle:@"设置" btnFontSize:14 titleColor:[UIColor blackColor]];
[settingBtn addTarget:self action:@selector(clickTheSettingBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:settingBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                   target:nil action:nil];
negativeSpacer.width = -20;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,rightBar,nil];

效果为

原文地址:https://www.cnblogs.com/Apologize/p/5764852.html