與您的自定義圖像創建UIButton
,然後創建一個與UIButton
作爲自定義視圖UIBarButtonItem
:
UIImage *buttonImage = [UIImage imageNamed:@"image"]; // Tint this image programmatically
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
請注意,這主要針對iOS 6及以下版本。在iOS 7+中,您可以通過tintColor
免費獲得此行爲。
可能重複的[我可以有一個UIBarButtonItem與彩色圖像?](http://stackoverflow.com/questions/1835260/can-i-have-a-uibarbuttonitem-with-a-colored-image) –
+1。從未注意到這件事。 – Bhavin