使用自定义图像和文本颜色更改后退按钮 [英] change backbutton with custom image and textcolor

查看:80
本文介绍了使用自定义图像和文本颜色更改后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在导航栏中更改后退按钮的外观.为此,我有这段代码.

I want to change the backbutton appearance for my backbutton in my navigation bar. For that I have this piece of code.

UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.backgroundColor = [UIColor colorWithPatternImage:backButtonImage];
backbutton.titleLabel.text = @"back";
backbutton.titleLabel.textColor= [UIColor colorWithRed:50/255.0
                                                 green:158/255.0
                                                  blue:218/255.0
                                                 alpha:1.0];

backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;

但是我仍然得到标准的后退按钮.有帮助吗?

But still I get the standard back button. Any help ?

推荐答案

使用以下代码满足您的要求..

using bellow code for your requirement..

- (void)viewDidLoad
{
        ///write your code

        UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
        UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        //[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
        [backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
        [backbutton setTitle:@"Back" forState:UIControlStateNormal];
        backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
        UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
        [backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = back;
}

当后退按钮单击下面的方法调用时,您将转到上一个Viewcontroller

when back button clicked bellow method call and you will go to previous viewcontroller

-(void)back_Clicked{
      [self.navigationController popViewControllerAnimated:YES];
}

这篇关于使用自定义图像和文本颜色更改后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆