I have some very simple code to just programmatically set a button's background image to something but when the simulator runs it shows nothing. On the storyboard it shows that the image is present however, and again nothing shows.
class ViewController: UIViewController {
#IBOutlet weak var LetUsOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let backgroundColor = UIColor(red: 0, green: 255, blue: 247, alpha: 1)
self.view.backgroundColor = backgroundColor
let backgroundButtonImage = UIImage(named: "round rectangle button.png") as UIImage?
self.LetUsOutlet.setImage(backgroundButtonImage, for: .normal)
}
#IBAction func LetUsCreateMeal(_ sender: UIButton) {
}
#IBAction func CreateYourOwn(_ sender: UIButton) {
}
#IBAction func BrowseItems(_ sender: UIButton) {
}
#IBAction func ViewYourMeals(_ sender: UIButton) {
}
}
This is the simple code that I have so I am not sure why it isn't working properly. Below is my storyboard.
http://imgur.com/gallery/c5EWW
The first button is where I try to programatically set the background, and the other 3 are when I just set the background image property within the storyboard.
This is what happens when it runs, showing that all background images are empty.
http://imgur.com/gallery/urM25
Any help is appreciated.
EDIT: I seemed to have the image stored in the wrong place, I put it in a separate folder and not in the xassets folder.
To change button background image you should use
LetUsOutlet.setBackgroundImage(UIImage(named:"round rectangle button.png"), forState: UIControlState.Normal)
I mocked up an example - seemed to work okay for me. I suggest you check your button's constraints in IB
change this line
let backgroundButtonImage = UIImage(named: "round rectangle button.png") as UIImage?
to this line
let backgroundButtonImage = UIImage(named: "round rectangle button")
and this line
self.LetUsOutlet.setImage(backgroundButtonImage, for: .normal)
to this
self.LetUsOutlet.setBackgroundImage(backgroundButtonImage, for: .normal)
Related
NOTE: Others have asked a similar question and none of the answers provided solved my problem.
Here's my code:
#IBOutlet var testButton:UIButton!
override func viewDidLoad() {
self.testButton.setImage(UIImage(named: "icn_checkbox_")?.imageWithColor(color: CMStyle.darkBlueColor), for: .normal)
self.testButton.setImage(UIImage(named: "icn_checked")?.imageWithColor(color: CMStyle.darkBlueColor), for: .selected)
}
#IBAction func testButtonAction(_ sender: Any) {
print("INSIDE testButtonAction")
self.testButton.isSelected = !self.testButton.isSelected
print("self.testButton.isSelected: ",self.testButton.isSelected)
}
This is the button:
.
These are the characteristics of the button:
.
This is icn_checkbox_ image used for .normal state:
This is icn_checked image used for .selected state:
The code inside testButtonAction gets executed. So I don't really see why wouldn't the image change (Why doesn't the button become checked).
You can use this:
#IBOutlet var testButton: UIButton!
override func viewDidLoad() {
self.testButton.setImage(UIImage(named: "icn_checkbox_")?.imageWithColor(color: CMStyle.darkBlueColor), for: .normal)
}
#IBAction func testButtonAction(_ sender: Any) {
print("INSIDE testButtonAction")
self.testButton.isSelected = !self.testButton.isSelected
if self.testButton.isSelected {
self.testButton.setImage(UIImage(named: "icn_checked")?.imageWithColor(color: CMStyle.darkBlueColor), for: .normal)
} else {
self.testButton.setImage(UIImage(named: "icn_checkbox_")?.imageWithColor(color: CMStyle.darkBlueColor), for: .normal)
}
}
You can set images for button states just in Storyboard, where Default = .normal and Selected = .selected
Choose selected and set image there. Image with color is similar to tintColor if You're using template images
newGameButton.setTitleColor(UIColor.black, for: .normal) does not work on an outlet. It only works for me on an action where the button is the sender of the action.
Every answer I have found addresses just using setTitleColor as the solution to setting a UIButton's text color, but not the actual problem of it not working.
#IBOutlet var newGameButton: UIButton!
override func viewDidLoad()
super.viewDidLoad()
newGameButton.setTitleColor(UIColor.black, for: .normal)
}
#IBAction func touchCard(_ sender: UIButton) {
newGameButton.setTitleColor(UIColor.black, for: .normal)
}
It seems like when I set the color to black in viewDidLoad(),
it actually changes the color of the button for state selected
but does nothing else.
The desired changed does happen when you do setTitleColor on
the sender in the touchCard method.
I'm looking for a way to change background color only for buttons that have the same text. So I created (at this time only 2) IBOutletCollections and 1 IBAction to test. But I have 27 different button numbers to code... or do I need to create 27 Outlet Collections and 27 Actions?
class SecondViewController: UIViewController {
#IBOutlet var button1color: [UIButton]!
#IBOutlet var button2color: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func bouton1(_ sender: UIButton) {
for button in self.button1color {
if button.backgroundColor == nil {
button.backgroundColor = UIColor(displayP3Red: 1, green: 1, blue: 0.5, alpha: 0.8)
} else {
button.backgroundColor = nil
}
}
}
Is there a way to create a function to avoid typing 27 times the same code for each case?
For more informations, feel free to ask me!
Thank you for your help.
I'm looking for a way to change background color only for buttons that have the same text
I understand it like this: You need collection of buttons and when some button from this collection is pressed, you want to change color for all buttons from collection which have the same text (title when we're speaking about UIButton)
So you can have just one collection for all buttons. Link them to this collection
#IBOutlet var buttons: [UIButton]!
and also you can have just one action for all buttons. Also link them to this action. Then when button is pressed, change backgroundColor of button which has the same title as button which's been pressed
#IBAction func buttonPressed(_ sender: UIButton) {
for button in buttons where button.currentTitle == sender.currentTitle {
button.backgroundColor = UIColor(displayP3Red: 1, green: 1, blue: 0.5, alpha: 0.8)
}
}
It is working for navigationBar:
var colour = UIColor.red
self.navigationController?.navigationBar.tintColor = colour
But do not work for toolbar:
self.navigationController?.toolbar.tintColor = colour
I searched the internet and stack overflow. No answer is workable for me.
Some people said:
self.toolbar.barTintColor = UIColor.redColor()
It is also not working for me. (value of type 'thisView' has no member 'toolbar')
I want to edit the toolbar color in coding. No change in the storyboard setting. Thanks.
EDIT:
I am working on adding a toolbar under the webview. Like go back, stop, reload.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
var colour = UIColor.red
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = colour
// problem in here ..........................
self.navigationController?.toolbar.tintColor = UIColor.black
let URL = NSURL(string: "https://www.apple.com")
webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)
}
#IBAction func backButton(_ sender: AnyObject) {
webView.goBack()
}
#IBAction func nextButton(_ sender: AnyObject) {
webView.goForward()
}
#IBAction func refreshButton(_ sender: AnyObject) {
webView.reload()
}
#IBAction func stopbutton(_ sender: AnyObject) {
webView.stopLoading()
}
}
If you've just got a toolbar that is on a ViewController in Your Storyboard all you need to do is add an IBOutlet to your View Controller and connect the toolbar in the storyboard to that outlet. This code goes in your ViewController
#IBOutlet var toolbar: UIToolbar?
Then, in the storyboard, hold the control button and click drag from View Controller (in the left sidebar) to your toolbar. This will create a connection between the toolbar in the storyboard to the toolbar var in your code. After that connection is made all you need to do is set the barTintColor on that toolbar variable like so:
self.toolbar.barTintColor = UIColor.blue
I wrote this function a few days ago to kinda workaround the fact that you can't change the color.
func setStatusBarColor(){
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 81/255, green: 184/255, blue: 222/255, alpha: 1)
self.view.addSubview(view)
}
Also make sure to set the statusbar to .lightContent
UIApplication.sharedApplication().statusBarStyle = .LightContent
I have this toolbar in my navigation controller. Now what I am trying to do is when the user selects an item (UIBarButtonItem) in my toolbar, have that item highlighted with a background colour until either the user deselects the item or selects another item. How would I do this?
Here are my selector methods for each item of the toolbar, I connected them via storyboard:
#IBAction func addText(sender: AnyObject) {
annotationSelected = 3
}
#IBAction func drawCircle(sender: AnyObject) {
annotationSelected = 1
}
#IBAction func drawRectangle(sender: AnyObject) {
annotationSelected = 2
}
#IBAction func drawStamp(sender: AnyObject) {
annotationSelected = 4
}
This is all I have done. Here is a screenshot of my toolbar:
Here is what I got:
#IBOutlet var textToolButton: UIBarButtonItem!
#IBOutlet var circleToolButton: UIBarButtonItem!
#IBOutlet var rectangleToolButton: UIBarButtonItem!
#IBOutlet var stampToolButton: UIBarButtonItem!
then
textToolButton.target = self
textToolButton.style = .Done
textToolButton.action = #selector(ViewController.barButtonPressed)
let selectedBackgroundColor = UIImage(color: .redColor())
textToolButton.setBackgroundImage(selectedBackgroundColor, forState: UIControlState.Highlighted, style: .Done, barMetrics: UIBarMetrics.Default)
and then the method
func barButtonPressed(sender: UIBarButtonItem) {
print(sender)
annotationSelected = sender.tag
}
background is still not changing color
I find a same question. May be can help you.
custom-pressed-uibarbuttonitem-backgrounds
I find a easy method to do. You can dray a button to the toolBar,and you will see like this.
And you should change the button's type and Image.
storyboard screenshot
then you should link the button to your viewController.
#IBOutlet weak var textToolButton: UIButton!
and you can do.
let selectedBackgroundColor = UIImage(color: .redColor())
textToolButton.setBackgroundImage(selectedBackgroundColor, forState: .Highlighted)
May be I can help you.
The cleanest way you could do it, is create an overall function where you pass in the button that's been selected. Something like this:
var allButtons = [button1, button2, button3, button4]
func resetTabBar (buttonSelected:UIButton) {
for button in allButtons {
if button == buttonSelected {
button.backgroundColor = "Black"
}
else {
button.backgroundColor = "Blue"
}
}
}
And then in your functions you've created, just pass in the sender like so:
#IBAction func addText(sender: AnyObject) {
resetTabBar(sender)
}
Note: This is assuming you have outlets for all of your buttons. If you don't, add them.