“开启生长之旅!这是我参与「日新计划 2 月更文应战」的第 21 天,点击检查活动详情”
前语
比如:调整BarButtonItem按钮和titleView的距离
1、原理:
titleview的起点方位和尺度依赖于leftBarButtonItem和rightBarButtonItem的方位
。2、设置titleview之前,先初始化leftBarButtonItem和rightBarButtonItem的方位,然后依据leftBarButtonItem和rightBarButtonItem的方位来使titleview居中。
常见问题
1、 BarButtonItem 隐藏失效的解决方案:
运用initWithCustomView进行实例化BarButtonItem
2、 iOS13.5.1 版别无法点击导航条右侧按钮:CustomView 不能直接是UIButton, 因而解决方案只需
对UIButton进行包装一层之后再作为CustomView
I、调整BarButtonItem按钮和titleView的距离
与屏幕鸿沟 或者与titleView 的距离 只需分别调整rightBarButtonItems 数组元素的次序。
首要利用UIBarButtonItem 的UIBarButtonSystemItemFixedSpace 体系控件
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
/**
* width为负数时,相当于btn向右移动width数值个像素,因为按钮本身和鸿沟距离为5pix,所以width设为-5时,距离正好调整
* 为0;width为正数时,正好相反,相当于往左移动width数值个像素
*/
negativeSpacer.width = 10;
1.1 调整右边按钮和titleView的距离
//设置右边按钮
UIBarButtonItem *btn_right = [UIBarButtonItem barButtonItemWithTarget:self Image:@"in" highlightedImage:@"in" actionMethod:@selector(in)];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
/**
* width为负数时,相当于btn向右移动width数值个像素,因为按钮本身和鸿沟距离为5pix,所以width设为-5时,距离正好调整
* 为0;width为正数时,正好相反,相当于往左移动width数值个像素
*/
negativeSpacer.width = 10;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: btn_right, negativeSpacer,nil];
1.2 调整左面按钮和titleView的距离
// 经过测验,发现titleview的起点方位和尺度依赖于leftBarButtonItem和rightBarButtonItem的方位。
UILabel * centerView = [[UILabel alloc] initWithFrame:CGRectMake(kAdjustRatio(20),0 , self.view.frame.size.width- kAdjustRatio(20), self.navigationController.navigationBar.frame.size.height)];
self.navigationItem.titleView = centerView;
// 设置titleview之前,先初始化leftBarButtonItem和rightBarButtonItem的方位,然后依据leftBarButtonItem和rightBarButtonItem的方位来使titleview居中。
// self.navigationItem.titleView.
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
/**
* width为负数时,相当于btn向右移动width数值个像素,因为按钮本身和鸿沟距离为5pix,所以width设为-5时,距离正好调整
* 为0;width为正数时,正好相反,相当于往左移动width数值个像素
*/
negativeSpacer.width = 10;
// self.navigationItem.leftBarButtonItem =negativeSpacer;
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects: negativeSpacer,nil];
1.3 设置webViewVC 导航条的左面按钮的方位
- (void)updateNavigationItems {
[self.navigationItem setLeftBarButtonItems:nil animated:NO];
if (self.webView.canGoBack/* || self.webView.backForwardList.backItem*/) {// Web view can go back means a lot requests exist.
UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceButtonItem.width = -6.5;
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
if (self.navigationController.viewControllers.count == 1) {
NSMutableArray *leftBarButtonItems = [NSMutableArray arrayWithArray:@[spaceButtonItem,self.navigationBackBarButtonItem]];
// If the top view controller of the navigation controller is current vc, the close item is ignored.
if (self.showsNavigationCloseBarButtonItem && self.navigationController.topViewController != self){
[leftBarButtonItems addObject:self.navigationCloseBarButtonItem];
}
[self.navigationItem setLeftBarButtonItems:leftBarButtonItems animated:NO];
} else {
if (self.showsNavigationCloseBarButtonItem){
[self.navigationItem setLeftBarButtonItems:@[self.navigationCloseBarButtonItem] animated:NO];
}else{
[self.navigationItem setLeftBarButtonItems:@[] animated:NO];
}
}
} else {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
[self.navigationItem setLeftBarButtonItems:nil animated:NO];
}
}
II、相关的常见问题
2.1 self.navigationItem.leftBarButtonItem.customView.hidden=YES
失效
- UIBarButtonItem 运用
initWithCustomView
进行实例化的时,这个方法才收效
self.navigationItem.leftBarButtonItem.customView.hidden=YES
- 解决方法: 运用
initWithCustomView
进行实例化
UIBarButtonItem *lefttItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = lefttItem;
2.2 iOS13.5.1 版别无法点击导航条右侧按钮
- 无法点击代码
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// [btn setTitle:QCTLocal(@"print") forState:UIControlStateNormal];
// [btn setTitleColor:HWColor(51, 51, 51) forState:UIControlStateNormal];
// btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
// [btn addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchUpInside];
// btn.tag = 0;
// btn.tintColor = [UIColor blackColor];
// UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
// rightBtn.tintColor = [UIColor blackColor];
// self.navigationItem.rightBarButtonItem = rightBtn;
解决方案
CustomView 不能直接是UIButton, 因而解决方案只需对UIButton进行包装一层即可
- (void)setupNavigationBar {
// 设置导航条右侧的按钮
UIButton *btn = [[UIButton alloc] init];
// [btn setImage:[UIImage imageNamed:@"icon_kaidan_huiyuan"] forState:UIControlStateNormal];
[btn setTitle:QCTLocal(@"print") forState:UIControlStateNormal];
[btn setTitleColor:rgb(51,51,51) forState:UIControlStateNormal];
[btn addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchDown];
btn.titleLabel.font = kTextFont(14);
[btn sizeToFit];
UIView *btnView = [[UIView alloc] init];
btnView.frame = btn.bounds;
[btnView addSubview:btn];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btnView];
item.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = item;
}
2.3 运用 [UIBarButtonItem alloc] initWithImage: 方法,导致图片颜色被冲的解决方案
从其他VC回到当时控制器的时候,发现右边的self.navigationItem.rightBarButtonItem的布景颜色被减弱了
- 解决方法:UIImageRenderingModeAlwaysOriginal
UIImage *aimage = [UIImage imageNamed:@"icon_right_menu"];
UIImage *image = [aimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(moreAction)];
[self.navigationItem setRightBarButtonItem:rightItem animated:YES];
III、 控制器的跳转和回退细节处理
3.1 UIActionSheet翻开相册的处理:通常是等didDismissWithButtonIndex 之后才进行控制器跳转
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{
if (buttonIndex == 0) {
[self createCamera];
}
}
3.2 pop回栈中指定的VC
- (void)pop
{
for (UIViewController *temp in self.navigationController.viewControllers) {
if ([temp isKindOfClass:[QCTMyFinancialViewController class]]) {
[self.navigationController popToViewController:temp animated:YES];
}
}
}
IV、see also
常用的扩展方法
UIBarButtonItem (Extension)
@implementation UIBarButtonItem (Extension)
+ (UIBarButtonItem*)barButtonItemWithTarget:(id)target Image:(NSString*)imageName highlightedImage:(NSString*)highlightedImage actionMethod:(SEL)actionMethod{
// 设置图片
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
//设置frame
button.size = button.currentBackgroundImage.size;
//设置监听
[button addTarget:target action:actionMethod forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc]initWithCustomView:button];
}