如何更改导航项(按钮)的背景图像? [英] How to change background image for navigation item(button)?

查看:34
本文介绍了如何更改导航项(按钮)的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每个导航项(按钮)(通用)设置背景图像.

I want to set background image for every navigation item(button) (universal).

这些图片应该是什么样子的?

How should these images look?

是否有固定尺寸或可以创建重复的背景图像?

Are there fixed dimensions or it is possible to create repeating background image?

如何以编程方式更改此背景图像?

How to programmatically change then this background image?

感谢帮助

更新:

我找到了用于更改此按钮背景的代码,但它不起作用.

I found this code for changing background of this button but it's no working.

UIImage *barButton = [UIImage imageNamed:@"button_background.png"];
    [[UIBarButtonItem appearance] setBackgroundImage:barButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

而且我不知道应该是哪个尺寸.

And I don't know which dimensions should be.

附言我想改变按钮的背景就像你可以改变背景颜色一样(self.navigation.controller.nvigationBar.tintColor..)

P.S. I want to change background of button just like you can change background color (self.navigation.controller.nvigationBar.tintColor..)

推荐答案

下面的代码或许能解决你的问题

The code below might solve your problem

UIButton *backBtn     = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"button_background.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = cancelButton;

这篇关于如何更改导航项(按钮)的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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