How to put an image in a button by code in Swift?How to call Objective-C code from Swift#pragma mark in Swift?How to detect tableView cell touched or clicked in swiftSwift 2 iOS 9 animation disappears after button text changedunable to add SSRadioButtonsController radio button title from codeProblems storing images in NSUserDefaults to show them in a tableView. SwiftSwift. Changing multiple UIButtons with IBOutletHow do I pass data from a UIViewController to UITabBarController?Swift Error - Use of undeclared type 'cell' - Collection ViewBMI app print result is 0 even with variable being hard coded

Why must Chinese maps be obfuscated?

Mistake in years of experience in resume?

Older movie/show about humans on derelict alien warship which refuels by passing through a star

Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?

Extracting Dirichlet series coefficients

Help with my training data

What is the term for a person whose job is to place products on shelves in stores?

How do I produce this symbol: Ϟ in pdfLaTeX?

Is Diceware more secure than a long passphrase?

What is this word supposed to be?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

Negative Resistance

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

Why did C use the -> operator instead of reusing the . operator?

How to not starve gigantic beasts

std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?

How to pronounce 'c++' in Spanish

Why did Rep. Omar conclude her criticism of US troops with the phrase "NotTodaySatan"?

Is there metaphorical meaning of "aus der Haft entlassen"?

Creating a chemical industry from a medieval tech level without petroleum

Why do real positive eigenvalues result in an unstable system? What about eigenvalues between 0 and 1? or 1?

How to find if a column is referenced in a computed column?

Does the damage from the Absorb Elements spell apply to your next attack, or to your first attack on your next turn?



How to put an image in a button by code in Swift?


How to call Objective-C code from Swift#pragma mark in Swift?How to detect tableView cell touched or clicked in swiftSwift 2 iOS 9 animation disappears after button text changedunable to add SSRadioButtonsController radio button title from codeProblems storing images in NSUserDefaults to show them in a tableView. SwiftSwift. Changing multiple UIButtons with IBOutletHow do I pass data from a UIViewController to UITabBarController?Swift Error - Use of undeclared type 'cell' - Collection ViewBMI app print result is 0 even with variable being hard coded






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to make a menu that depends on the kind of profile the user has, so I'm trying to set an image on the buttons. I did it this way:



import UIKit

class ViewController: UIViewController

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var txtNum: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

@IBAction func btnSend(_ sender: Any)
var Num = txtNum.text!
if Num == "1"
btn1.setImage(UIImage(named:"museos.png"), for: .normal)
btn2.setImage(UIImage(named: "otros.png"), for: .normal)
else if Num == "2"
btn1.setImage(UIImage(named:"parques.png"), for: .normal)
btn2.setImage(UIImage(named: "monumentos.png"), for: .normal)





But, once the app runs, it shows me the buttons like this:



screenshot



and I don't know what is happening here. Some help?










share|improve this question
























  • Could you describe what you're expecting to see?

    – shim
    Mar 22 at 15:52











  • I'm supose to see an image that make a reference to a mall icon for example, so every button is a different category like a park, a Hospital or a museum

    – Alejandrina
    Mar 22 at 16:11







  • 1





    Might help if you show some more of your code then.

    – shim
    Mar 22 at 16:15

















0















I'm trying to make a menu that depends on the kind of profile the user has, so I'm trying to set an image on the buttons. I did it this way:



import UIKit

class ViewController: UIViewController

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var txtNum: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

@IBAction func btnSend(_ sender: Any)
var Num = txtNum.text!
if Num == "1"
btn1.setImage(UIImage(named:"museos.png"), for: .normal)
btn2.setImage(UIImage(named: "otros.png"), for: .normal)
else if Num == "2"
btn1.setImage(UIImage(named:"parques.png"), for: .normal)
btn2.setImage(UIImage(named: "monumentos.png"), for: .normal)





But, once the app runs, it shows me the buttons like this:



screenshot



and I don't know what is happening here. Some help?










share|improve this question
























  • Could you describe what you're expecting to see?

    – shim
    Mar 22 at 15:52











  • I'm supose to see an image that make a reference to a mall icon for example, so every button is a different category like a park, a Hospital or a museum

    – Alejandrina
    Mar 22 at 16:11







  • 1





    Might help if you show some more of your code then.

    – shim
    Mar 22 at 16:15













0












0








0








I'm trying to make a menu that depends on the kind of profile the user has, so I'm trying to set an image on the buttons. I did it this way:



import UIKit

class ViewController: UIViewController

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var txtNum: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

@IBAction func btnSend(_ sender: Any)
var Num = txtNum.text!
if Num == "1"
btn1.setImage(UIImage(named:"museos.png"), for: .normal)
btn2.setImage(UIImage(named: "otros.png"), for: .normal)
else if Num == "2"
btn1.setImage(UIImage(named:"parques.png"), for: .normal)
btn2.setImage(UIImage(named: "monumentos.png"), for: .normal)





But, once the app runs, it shows me the buttons like this:



screenshot



and I don't know what is happening here. Some help?










share|improve this question
















I'm trying to make a menu that depends on the kind of profile the user has, so I'm trying to set an image on the buttons. I did it this way:



import UIKit

class ViewController: UIViewController

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var txtNum: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

@IBAction func btnSend(_ sender: Any)
var Num = txtNum.text!
if Num == "1"
btn1.setImage(UIImage(named:"museos.png"), for: .normal)
btn2.setImage(UIImage(named: "otros.png"), for: .normal)
else if Num == "2"
btn1.setImage(UIImage(named:"parques.png"), for: .normal)
btn2.setImage(UIImage(named: "monumentos.png"), for: .normal)





But, once the app runs, it shows me the buttons like this:



screenshot



and I don't know what is happening here. Some help?







ios swift uibutton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 18:18









shim

4,16164880




4,16164880










asked Mar 22 at 15:50









AlejandrinaAlejandrina

33




33












  • Could you describe what you're expecting to see?

    – shim
    Mar 22 at 15:52











  • I'm supose to see an image that make a reference to a mall icon for example, so every button is a different category like a park, a Hospital or a museum

    – Alejandrina
    Mar 22 at 16:11







  • 1





    Might help if you show some more of your code then.

    – shim
    Mar 22 at 16:15

















  • Could you describe what you're expecting to see?

    – shim
    Mar 22 at 15:52











  • I'm supose to see an image that make a reference to a mall icon for example, so every button is a different category like a park, a Hospital or a museum

    – Alejandrina
    Mar 22 at 16:11







  • 1





    Might help if you show some more of your code then.

    – shim
    Mar 22 at 16:15
















Could you describe what you're expecting to see?

– shim
Mar 22 at 15:52





Could you describe what you're expecting to see?

– shim
Mar 22 at 15:52













I'm supose to see an image that make a reference to a mall icon for example, so every button is a different category like a park, a Hospital or a museum

– Alejandrina
Mar 22 at 16:11






I'm supose to see an image that make a reference to a mall icon for example, so every button is a different category like a park, a Hospital or a museum

– Alejandrina
Mar 22 at 16:11





1




1





Might help if you show some more of your code then.

– shim
Mar 22 at 16:15





Might help if you show some more of your code then.

– shim
Mar 22 at 16:15












2 Answers
2






active

oldest

votes


















1














Remove the .png extensions from your image names.



You can also try changing your button type from system to custom. Setting the image requires using the setImage function.



button.setImage(UIImage(named: "museos"), for: .normal)





share|improve this answer























  • thank you, it works

    – Alejandrina
    Mar 22 at 17:37


















-2














You just have to use the imageView property like this :



btn1.imageView?.image = UIImage(named: "...")





share|improve this answer























  • When I implement this nothing happens, I mean the buttons don't change

    – Alejandrina
    Mar 22 at 16:45











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303373%2fhow-to-put-an-image-in-a-button-by-code-in-swift%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Remove the .png extensions from your image names.



You can also try changing your button type from system to custom. Setting the image requires using the setImage function.



button.setImage(UIImage(named: "museos"), for: .normal)





share|improve this answer























  • thank you, it works

    – Alejandrina
    Mar 22 at 17:37















1














Remove the .png extensions from your image names.



You can also try changing your button type from system to custom. Setting the image requires using the setImage function.



button.setImage(UIImage(named: "museos"), for: .normal)





share|improve this answer























  • thank you, it works

    – Alejandrina
    Mar 22 at 17:37













1












1








1







Remove the .png extensions from your image names.



You can also try changing your button type from system to custom. Setting the image requires using the setImage function.



button.setImage(UIImage(named: "museos"), for: .normal)





share|improve this answer













Remove the .png extensions from your image names.



You can also try changing your button type from system to custom. Setting the image requires using the setImage function.



button.setImage(UIImage(named: "museos"), for: .normal)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 16:56









HamerHamer

8821530




8821530












  • thank you, it works

    – Alejandrina
    Mar 22 at 17:37

















  • thank you, it works

    – Alejandrina
    Mar 22 at 17:37
















thank you, it works

– Alejandrina
Mar 22 at 17:37





thank you, it works

– Alejandrina
Mar 22 at 17:37













-2














You just have to use the imageView property like this :



btn1.imageView?.image = UIImage(named: "...")





share|improve this answer























  • When I implement this nothing happens, I mean the buttons don't change

    – Alejandrina
    Mar 22 at 16:45















-2














You just have to use the imageView property like this :



btn1.imageView?.image = UIImage(named: "...")





share|improve this answer























  • When I implement this nothing happens, I mean the buttons don't change

    – Alejandrina
    Mar 22 at 16:45













-2












-2








-2







You just have to use the imageView property like this :



btn1.imageView?.image = UIImage(named: "...")





share|improve this answer













You just have to use the imageView property like this :



btn1.imageView?.image = UIImage(named: "...")






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 16:33









Sombre Osmo'zSombre Osmo'z

345




345












  • When I implement this nothing happens, I mean the buttons don't change

    – Alejandrina
    Mar 22 at 16:45

















  • When I implement this nothing happens, I mean the buttons don't change

    – Alejandrina
    Mar 22 at 16:45
















When I implement this nothing happens, I mean the buttons don't change

– Alejandrina
Mar 22 at 16:45





When I implement this nothing happens, I mean the buttons don't change

– Alejandrina
Mar 22 at 16:45

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303373%2fhow-to-put-an-image-in-a-button-by-code-in-swift%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해