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

本文介绍了如何在iOS应用中自定义导航栏,包括设置导航栏背景颜色,整体调整字体颜色,自定义返回按钮,以及在UIViewController中修改导航栏左侧和右侧按钮的方法,同时讲解了如何调整导航栏两侧按钮与边距的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:导航栏的背景颜色如何设置
控制器是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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值