我想製作一個自定義的UIBarButtonItem來表示完成按鈕。 我想使應用程序國際化,所以我希望文本可以直接編輯。要考慮「完成」標籤的不同長度,我設計了一個StretchableImage。是否有可能直接更改默認的編輯按鈕背景或是需要自定義的UIBarButtonItem?如果是這樣,根據標籤長度動態調整背景拉伸圖片大小的方法是什麼?更改完成UIBarButtonItem的背景圖像
1
A
回答
7
如果您僅針對iOS 5.0,則可以使用新的UIAppearance
方法更改默認外觀,具體爲-setBackButtonBackgroundImage:forState:barMetrics:
。
如果您需要支持舊版本的iOS,你應該繼承UIBarButtonItem
,加UIButton
實例變量,創建並在UIBarButtonItem
的init
方法調用–initWithCustomView:
。這是因爲UIBarButtonItem
不是UIView
的子類,您不能在其中繪製自定義圖像。您還應該手動設置UIBarButtonItem
的width
屬性。
@interface MYCustomBarButtonItem : UIBarButtonItem
{
UIButton *button;
}
- (id)initWithTitle:(NSString *)title; // name it in concordance with your needs
@end
#define kButtonHeight 30
#define kButtonTitleFontSize 12
#define kButtonTitlePadding 5
@implementation MYCustomBarButtonItem
- (id)initWithTitle:(NSString *)title
{
button = [UIButton buttonWithType:UIButtonTypeCustom]; // add retain here, and write the dealloc method if you aren't using ARC. Also, release it if self == nil.
self = [super initWithCustomView:button];
if (self) {
UIFont *titleFont = [UIFont boldSystemFontOfSize:kButtonTitleFontSize];
CGSize titleSize = [title sizeWithFont:titleFont];
CGFloat buttonWidth = titleSize.width + kButtonTitlePadding * 2;
button.frame = CGRectMake(0, 0, buttonWidth, kButtonHeight);
self.width = buttonWidth;
[button setTitle:title forState:UIControlStateNormal];
// Set your styles
button.titleLabel.font = titleFont;
// Normal state background
UIImage *backgroundImage = ...; // create your normal stretchable background image
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
// Pressed state background
backgroundImage = ...; // create your pressed stretchable background image
[button setBackgroundImage:backgroundImage forState:UIControlStateHighlighted];
// and so on...
}
return self;
}
@end
PS。不要忘記覆蓋您的子類的target
和action
屬性,以便與button
實例配合使用。
2
我可能誤解了你的問題,但我相信所有你想要的是這樣的:
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"I am done" style:UIBarButtonItemStyleBordered target:self action:nil];
UIImage *stretchable = [[UIImage imageNamed:@"StretchableImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:16];
[btnDone setBackgroundImage:stretchable forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setRightBarButtonItem:btnDone];
一個的UIBarButtonItem並自動調整它的寬度與標籤的寬度。
+0
@Stanislav Yaglo是對的。此解決方案僅適用於iOS 5.如果您要定位舊版本,則需要製作UIBarButtonItem的簡單子類。 – simonbs 2012-01-18 21:04:40
相關問題
- 1. 使用圖像更改uibarbuttonitem背景
- 2. CSS重複的背景圖像的背景圖像完成
- 3. UIBarButtonItem - 以編程方式更改背景圖像
- 4. 如何更改UIBarButtonItem使用界面生成器創建的背景圖像
- 5. 搜索完成後jQuery背景圖像更改
- 6. 無法更改UIBarButtonItem的背景
- 7. 更改圖像背景
- 8. jquery更改背景圖像
- 9. onClick更改背景圖像
- 10. 將UIBarButtonItem設置爲無背景圖像
- 11. 更改背景圖像像滑塊
- 12. 動態更改圖像視圖背景
- 13. 編輯/完成按鈕,更改完成按鈕背景顏色
- 14. 更改div的背景圖像ERROR
- 15. 更改背景圖像的序列
- 16. 更改元素的背景圖像
- 17. 更改UIToolbar的背景圖像
- 18. 的UIView更改背景顏色圖像
- 19. 更改TTThumbsViewController的背景顏色/圖像
- 20. 更改MFMailComposeViewController的背景圖像
- 21. 更改Imagebutton的背景圖像
- 22. 更改Android活動的背景圖像
- 23. 動態更改背景圖像的值
- 24. 懸停時的背景圖像更改
- 25. onMouseOver更改背景圖像的位置
- 26. 如何更改TD的背景圖像?
- 27. GTK更改窗口的背景圖像
- 28. 更改按鈕的背景圖像?
- 29. 更改Images.xcassets圖像的背景顏色?
- 30. jquery更改懸停的背景圖像
如何根據文字的寬度來調整按鈕大小。另外,你如何將背景設置爲StretchableImage?謝謝。 – 2012-01-18 21:15:39
您可以使用'NSString'的'-sizeWithFont:'方法:http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html。背景是用'UIButton'的'-setBackgroundImage:forState:'設置的。 – 2012-01-18 21:19:59
@ibeitia,我爲你更新了代碼。 – 2012-01-18 21:46:38