3 回答

TA貢獻1772條經驗 獲得超5個贊
目標C
UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
斯威夫特4
let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)

TA貢獻1776條經驗 獲得超12個贊
Mike的解決方案將僅顯示圖像,但是在按鈕上設置的任何標題將不可見,因為您可以設置標題或圖像。
如果要同時設置(您的圖像和標題),請使用以下代碼:
btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];

TA貢獻1810條經驗 獲得超4個贊
在此之前,我必須根據圖像幀的大小顯式調整按鈕幀的大小。
UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;
[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController
action:@selector(revealToggle:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:listButton];
self.navigationItem.leftBarButtonItem = jobsButton;
- 3 回答
- 0 關注
- 733 瀏覽
添加回答
舉報