亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在代碼中為UIButton設置圖像

在代碼中為UIButton設置圖像

拉莫斯之舞 2019-11-25 14:16:41
如何在代碼中為UIButton設置圖像?我有這個:UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnTwo.frame = CGRectMake(40, 140, 240, 30);[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btnTwo];但看不到將為其設置圖像的內容。
查看完整描述

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)


查看完整回答
反對 回復 2019-11-25
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

Mike的解決方案將僅顯示圖像,但是在按鈕上設置的任何標題將不可見,因為您可以設置標題或圖像。


如果要同時設置(您的圖像和標題),請使用以下代碼:


btnImage = [UIImage imageNamed:@"image.png"];

[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];

[btnTwo setTitle:@"Title" forState:UIControlStateNormal];


查看完整回答
反對 回復 2019-11-25
?
蝴蝶不菲

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;


查看完整回答
反對 回復 2019-11-25
  • 3 回答
  • 0 關注
  • 733 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號