如何改变barButtonItem的颜色?

时间:2011-09-14 12:37:29

标签: iphone objective-c xcode uinavigationbar

在navigationBar上作为rightBarButtonItem我通过以下编码获取编辑按钮

self.navigationItem.rightBarButtonItem=self.editButtonItem;

但我想更改编辑按钮的颜色我的意思是我要更改rightBarButtonItem的颜色。我怎么能这样做?

请尽早回复。

提前完成。

3 个答案:

答案 0 :(得分:2)

您需要创建一个自定义按钮并使用initWithCustomView初始化您的UIBarButtonItem。

如果使用自定义图片,则按照自己的意愿创建UIButton,然后:

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];

答案 1 :(得分:1)

制作标准的UIBarButtonItem受到限制。但你可以使用你选择的任何UIView来启动它,例如标准的UIButton,您可以根据自己的喜好进行着色。

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 30, 20); // change as you like being constrained with navigation bar hieght
[myButton setTitle:@"My Button" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"Image for my button.png"] forState:UIControlStateNormal]; // <-- you can provide image instaed of title if you like
[myButton setBackgroundImage:[UIImage imageNamed:@"Background image for my button.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside]; // <-- don't forget configure this!!! 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];

答案 2 :(得分:0)

访问此链接。这是一个相同的答案。

specially indicating or highlighting a toolbar button when a note is added in iphone

使用方法[[UIBarButtonItem alloc] initWithCustomView:btnInfo];可以提供帮助。