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;
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:

and I don't know what is happening here. Some help?
ios swift uibutton
add a comment |
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:

and I don't know what is happening here. Some help?
ios swift uibutton
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
add a comment |
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:

and I don't know what is happening here. Some help?
ios swift uibutton
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:

and I don't know what is happening here. Some help?
ios swift uibutton
ios swift uibutton
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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)
thank you, it works
– Alejandrina
Mar 22 at 17:37
add a comment |
You just have to use the imageView property like this :
btn1.imageView?.image = UIImage(named: "...")
When I implement this nothing happens, I mean the buttons don't change
– Alejandrina
Mar 22 at 16:45
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
thank you, it works
– Alejandrina
Mar 22 at 17:37
add a comment |
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)
thank you, it works
– Alejandrina
Mar 22 at 17:37
add a comment |
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)
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)
answered Mar 22 at 16:56
HamerHamer
8821530
8821530
thank you, it works
– Alejandrina
Mar 22 at 17:37
add a comment |
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
add a comment |
You just have to use the imageView property like this :
btn1.imageView?.image = UIImage(named: "...")
When I implement this nothing happens, I mean the buttons don't change
– Alejandrina
Mar 22 at 16:45
add a comment |
You just have to use the imageView property like this :
btn1.imageView?.image = UIImage(named: "...")
When I implement this nothing happens, I mean the buttons don't change
– Alejandrina
Mar 22 at 16:45
add a comment |
You just have to use the imageView property like this :
btn1.imageView?.image = UIImage(named: "...")
You just have to use the imageView property like this :
btn1.imageView?.image = UIImage(named: "...")
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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