通过UIButton的Hidden属性隐藏,估计没有比这更简单的了
-(UIButton *)createBarBtnItemWithTitle:(NSString *)title imageName:(NSString *)imageName isLeft:(BOOL)isLeft target:(id)target aSelector:(SEL)aSelector isHidden:(BOOL)isHidden{
if (!isLeft) {
if (imageName) {
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.frame = CGRectMake(0, 0, 30, 30);
rightBtn.exclusiveTouch = YES;
UIImage * image = [UIImage imageNamed:imageName];
[rightBtn setImage:image forState:UIControlStateNormal];
// [rightBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
[rightBtn addTarget:target action:aSelector forControlEvents:UIControlEventTouchUpInside];
rightBtn.titleLabel.text = imageName;//执行点击动作时做判断条件
rightBtn.tag = kTAG_BTN_RightItem;
rightBtn.hidden = isHidden;
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
self.navigationItem.rightBarButtonItem = rightItem;
return rightBtn;
}else{
CGSize titleSize = [self sizeWithText:title fontSize:15 maxWidth:kSCREEN_WIDTH];
// DDLog(@"size %@",NSStringFromCGSize(titleSize));
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
if (title.length <= 2) {
titleSize.width = 35;
}else{
rightBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
rightBtn.titleLabel.minimumScaleFactor = 1;
}
rightBtn.frame = CGRectMake(0, 0, titleSize.width, 30);
rightBtn.titleEdgeInsets = UIEdgeInsetsMake(-10, -20, -10, -20);
rightBtn.exclusiveTouch = YES;
[rightBtn setTitle:title forState:UIControlStateNormal];
[rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
// [rightBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
rightBtn.tag = kTAG_BTN_RightItem;
rightBtn.hidden = isHidden;
[rightBtn addTarget:target action:aSelector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
self.navigationItem.rightBarButtonItem = rightItem;
return rightBtn;
}
}else{
if (imageName) {
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 30, 30);
backBtn.exclusiveTouch = YES;
UIImage * image = [UIImage imageNamed:imageName];
[backBtn setImage:image forState:UIControlStateNormal];
// [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
[backBtn addTarget:target action:aSelector forControlEvents:UIControlEventTouchUpInside];
backBtn.titleLabel.text = imageName;//执行点击动作时做判断条件
backBtn.tag = kTAG_BTN_BackItem;
backBtn.hidden = isHidden;
UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = backBtnItem;
return backBtn;
}else{
if (!title) {
CGSize iconSize = CGSizeMake(18, 29);
iconSize = CGSizeMake(32, 32);
CGSize btnSize = CGSizeMake(30, 30);
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, btnSize.width, btnSize.height);
[backBtn setImage:[UIImage imageNamed:@"img_btnBack.png"] forState:UIControlStateNormal];
[backBtn setImageEdgeInsets:UIEdgeInsetsMake((btnSize.height - iconSize.height)/2.0, (btnSize.width - iconSize.width)/2.0, (btnSize.height - iconSize.height)/2.0, (btnSize.width - iconSize.width)/2.0)];
[backBtn addTarget:target action:aSelector forControlEvents:UIControlEventTouchUpInside];
backBtn.tag = kTAG_BTN_BackItem;
backBtn.hidden = isHidden;
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = leftItem;
return backBtn;
}else{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 30, 30);
[backBtn setImage:nil forState :UIControlStateNormal];
[backBtn setTitle:title forState:UIControlStateNormal];
[backBtn addTarget:target action:aSelector forControlEvents:UIControlEventTouchUpInside];
backBtn.tag = kTAG_BTN_BackItem;
backBtn.hidden = isHidden;
UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[backBtn addTarget:target action:aSelector forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = backBtnItem;
return backBtn;
}
}
}
}
网友评论