您的位置:首页 > 移动开发 > IOS开发

iOS 自定义导航栏背景,左侧按钮,右侧按钮,及标题

2017-04-01 15:15 417 查看
一:导航栏的背景颜色如何设置

控制器是UINavigationController

[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航栏背景图"] forBarMetrics:UIBarMetricsDefault];


可以利用下面方法的得到各种颜色的背景图片

+ (UIImage *)buttonImageFromColor:(UIColor *)color WithSize:(CGSize)size {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}


二:整体设置导航栏字体颜色

控制器是UINavigationController

[self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];


三:设置返回按钮

控制器是UINavigationController

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"FSNavfanhui"] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 44, 44);
// 让按钮内部的所有内容左对齐
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
// 修改导航栏左边的item
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// 隐藏tabbar
viewController.hidesBottomBarWhenPushed = YES;
}
// 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
[super pushViewController:viewController animated:animated];
}

- (void)back{
[self popViewControllerAnimated:YES];
}


四:UIViewController修改导航栏左侧按钮

控制器是UIViewController

#pragma mark - 设置返回按钮
- (void)setNavigationLeftBarTitleText:(NSString *)title{
FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44) Title:title TitleColor:HexRGB(0xffffff) BackgroundColor:[UIColor clearColor] Font:SYSTEM_FONT(14) State:UIControlStateNormal];
// 让按钮内部的所有内容左对齐
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

// 修改导航栏左边的item
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
#pragma mark - 返回点击事件
- (void)back{
[self.navigationController popViewControllerAnimated:YES];
}


四、五:UIViewController修改导航栏右侧按钮

.h中右侧按钮的block定义

/**
*  右侧按钮点击事件
*/
@property(nonatomic, copy) void (^fs_rightBtnClickBlock)(FSButton * fs_rightBtn);


1:根据文字定义导航栏右侧按钮

/**
* 根据文字设定右侧按钮
*/
- (void)setNavigationBarRightItemWithTitle:(NSString *)title actionBlock:(void (^)(FSButton *button))actionBlock{
FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44) Title:title TitleColor:HexRGB(0xffffff) BackgroundColor:[UIColor clearColor] Font:SYSTEM_FONT(14) State:UIControlStateNormal];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button addTarget:self action:@selector(fs_rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.fs_rightBtnClickBlock = actionBlock;
}

#pragma mark - 右侧按钮点击事件
- (void)fs_rightBtnClick:(FSButton *)sender{
self.fs_rightBtnClickBlock(sender);
}


2:根据图片定义导航栏右侧按钮

/**
* 根据图片设定右侧按钮方法
*/
- (void)setNavigationBarRightItemWithImage:(NSString *)imageName actionBlock:(void (^)(FSButton *))actionBlock{
FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44)];
[button setImage:IMAGE_NAMED(imageName) forState:UIControlStateNormal];
[button addTarget:self action:@selector(fs_rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.fs_rightBtnClickBlock = actionBlock;
}

#pragma mark - 右侧按钮点击事件
- (void)fs_rightBtnClick:(FSButton *)sender{
self.fs_rightBtnClickBlock(sender);
}


3:根据文字设置导航栏右侧的可点击和不可点击文字

/**
* 设定右侧按钮方法
*/
- (void)setNavigationBarRightItemWithTitleForNormal:(NSString *)titleForNormal titleForDisable:(NSString *)titleForDisable colorForNormal:(UIColor *)colorForNormal colorForDisable:(UIColor *)colorForDisable actionBlock:(void (^)(FSButton *))actionBlock{
FSButton *button = [[FSButton alloc] initWithFrame:FRAME(0, 0, 44, 44)];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button.titleLabel setFont:SYSTEM_FONT(14)];
[button setTitle:titleForNormal forState:UIControlStateNormal];
[button setTitleColor:colorForNormal forState:UIControlStateNormal];
[button setTitle:titleForDisable forState:UIControlStateDisabled];
[button setTitleColor:colorForDisable forState:UIControlStateDisabled];
[button addTarget:self action:@selector(fs_rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

//    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:colorForNormal, NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
//    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:colorForDisable, NSForegroundColorAttributeName,nil] forState:UIControlStateDisabled];
self.fs_rightBtnClickBlock = actionBlock;
}
#pragma mark - 右侧按钮点击事件
- (void)fs_rightBtnClick:(FSButton *)sender{
self.fs_rightBtnClickBlock(sender);
}


六:导航两侧按钮距离左右侧边距的修改

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame= CGRectMake(0, 0, 44, 44);
[btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];

/**
width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和  边界间距为5pix,所以width设为-5时,间距正好调整为0;width为正数 时,正好相反,相当于往左移动width数值个像素
*/
negativeSpacer.width = -5;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息