iOS 自定义UINavigationController返回按钮

本文介绍如何在iOS应用中自定义导航栏的返回按钮,包括设置按钮样式、添加触摸响应及显示自定义返回标题。

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

主要代码如下:

//自定义导航栏返回按钮
    self.navigationItem.leftBarButtonItem = ({
        
        //导航栏返回背景视图
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
        
        //返回按钮
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 12, 20, 20)];
        [button setBackgroundImage:[UIImage imageNamed:@"goBack"]
                          forState:UIControlStateNormal];
        [button addTarget:self
                   action:@selector(backTo:)
         forControlEvents:UIControlEventTouchDown];
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:button];
        
        //中间分割线
        UIView *seprateLine = ({
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30, 12, 1, 20)];
            view.backgroundColor = [UIColor colorWithRed:45/255.0 green:45/255.0 blue:45/255.0 alpha:1];
            view;
        });
        [view addSubview:seprateLine];
        
        //显示返回标题
        UILabel *backLabel = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 100, 44)];
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:14];
            label.textColor = [UIColor blackColor];
            label.text = @"ViewController";
            label;
        });
        [view addSubview:backLabel];
        
        //把view添加上去
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                          initWithCustomView:view];
        barButtonItem;
    });

 Demo代码下载:https://github.com/LuoDaRen/-

 

转载于:https://www.cnblogs.com/FightingLuoYin/p/4618373.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值