3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
目標(biāo)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貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
Mike的解決方案將僅顯示圖像,但是在按鈕上設(shè)置的任何標(biāo)題將不可見(jiàn),因?yàn)槟梢栽O(shè)置標(biāo)題或圖像。
如果要同時(shí)設(shè)置(您的圖像和標(biāo)題),請(qǐng)使用以下代碼:
btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
在此之前,我必須根據(jù)圖像幀的大小顯式調(diào)整按鈕幀的大小。
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 關(guān)注
- 740 瀏覽
添加回答
舉報(bào)