I have 2 UIViews that I converted to UIColorWells.
#IBOutlet weak var colorWell1: UIColorWell!
#IBOutlet weak var ColorWell2: UIColorWell!
When I run the app, I see the color wells just fine (see image below). However, when I try to click on them, the actual color picker never opens. It seems like its just the image of the color well showing up.
Is there method I have to create to make them open, or is it built in? This is what I have pertaining to the colorWells below:
override func viewDidLoad() {
super.viewDidLoad()
colorWell1.addTarget(self, action: #selector(colorWellChanged(_:)), for: .valueChanged)
colorWell2.addTarget(self, action: #selector(colorWellChanged(_:)), for: .valueChanged)
}
#objc func colorWellChanged(_ sender: Any) {
colorWell1.backgroundColor = colorWell1.selectedColor
colorWell2.backgroundColor = team2ColorWell.selectedColor
}
This is what the colorWells look like: enter image description here
This is my folder structure:
enter image description here
Related
I'm just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the image view is set perfectly fine. Do I have to set something in the properties of the button?
Here the code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageToSet = UIImage(named: "3-1")
b1.setBackgroundImage(imageToSet, for: .normal)
imageView.image = imageToSet
}
#IBOutlet weak var b1: UIButton!
#IBOutlet weak var imageView: UIImageView!
}
These are my setting for the button:
In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like
setBackgroundImage(..)
setImage(..)
This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default
I want the user to choose the preferred color by clicking the wanted button, and based on the choice it will be the background color of all pages in the App.
The code given is what I have used to change the background color through code, as shown in the image attached Screenshot of page sample . However, it changes temporary only and does not get saved when I move to another page and get back.
class ColorViewController: UIViewController {
#IBOutlet weak var redBtn: UIButton!
#IBOutlet weak var blueBtn: UIButton!
#IBAction func changeToRed(_ sender: UIButton) {
self.view.backgroundColor = UIColor.green
}
#IBAction func changeToBlue(_ sender: UIButton) {
self.view.backgroundColor = UIColor.cyan
}
If you also know how to save this color for a registered user (So every time the user signs in, the chosen background stays as is instead of reseting to default) it would be great. Please advise if you know the solution, I am new on Swift.
For saving user's color choice, you can use UserDefaults, that will allow you to store small sized information (like color preference) inside memory. So you can use something like this:
UserDefaults.standard.set(UIColor.red , forKey: "themePreferenceKey")
That will store Red choice in memory that'll be reachable with key themePreferenceKey later on.
For changing view/button background colors according to user's saved choice, you must fetch the value of themePreferenceKey whenever a view is Appeared, and change the colors accordingly. This can be implemented inside viewDidAppear method. Such as:
override func viewDidAppear(_ animated: Bool) {
if let preferredColor = UserDefaults.standard.object(forKey: "themePreferenceKey") as? UIColor {
self.view.backgroundColor = preferredColor
}
}
I have a button with icon, I want when pressed button icon fill with color and when pressed again empty color and return to the previous state
Note: As much as possible I do not want to change icon after every pressed.
Here is example how you can achieve that, let me know if you don’t understand something.
class ViewController: UIViewController {
#IBOutlet weak var someBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
}
//Setting up images for normal and selected state.
func configureUI() {
let image = UIImage(named: "image")
let imageFilled = UIImage(named: "image-filled")
someBtn.setImage(image, for: .normal)
someBtn.setImage(imageFilled, for: .selected)
}
#IBAction func someBtnPressed(_ sender: Any) {
// Toggle basically makes someBtn’s selected state either true or false when pressed
someBtn.isSelected.toggle()
}
}
You can call setImage(_:for:) method to set the image of the button for a particular state.
The button is at the normal state when the user is not pressing it, so you should do:
yourButton.setImage(hollowHeart, for: .normal)
The button is at the highlighted state when the user is touching it, so you should do:
yourButton.setImage(filledHeart, for: .highlighted)
You just need to do this once after you create the button, probably in viewDidLoad.
I am trying to do a custom toolbar that shows image and label but changes according to which view it is in. I have scoured the internet and read and tried everything I could find on the subject.
I figured that putting a tab bar was not a solution since it can't change for every view (or can it?).
Last time I asked I was marked duplicate and quoted Set image and title for bad button item?. I tried that but I could not get that to work at all. Ideally, I wanted a storyboard solution.
I'll show what I have done and tried with so far. What am I doing wrong? Have I missed something? I can't get the title to show on the toolbar item.
Xcode main.storyboard current toolbar version:
Sketch version (what I want it to look like):
What I hope it will change to in subsequent view:
Code:
class ViewController: UIViewController {
#IBOutlet weak var toolbar: UIToolbar!
#IBOutlet weak var leftBarButton: UIBarButtonItem!
var middleBarButton: UIBarButtonItem!
var rightBarButton: UIBarButtonItem!
var rightBarButton2: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
let button = UIButton(type: .system)
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "IconHelp"), for: .normal)
button.setTitle("Help", for: .normal)
button.addTarget(self, action: #selector(about), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
// var items = self.toolbar.items
// items![0] = UIBarButtonItem(customView: view)
leftBarButton = UIBarButtonItem(customView: button)
Any help would be much appreciated as I've tried to solve this all week. :P
I'm new to iOS development and I'm working on a Fire Safety App. I would like to give the user the option of 3 risk levels: Low, Medium and High by using (I suppose) 3 UIButtons which the user can tap, and which ever one they tap would turn a certain colour.
I've managed to get 3 UIButtons working, and they can change colour when tapped but if I tap another I can't get the previous button to change back and I've had difficulties getting a value for them (For example when submitted if the user pressed Low I would use the number 2 for calculations)
To show this I did a quick drawing:
Button Example
Thanks for your help :)
==== EDIT ====
I am now using a Segmented Control instead of UIButtons to do this. But I need to know how to use them in an if statement
#IBOutlet var RiskChoice: UISegmentedControl!
#IBOutlet var ValueLabel: UILabel!
#IBOutlet var CalcButton: UIButton!
#IBAction func Calculate(sender: UIButton) {
if (RiskChoice.selectedSegmentIndex == 0){
ValueLabel.text = String("Low")
}
Nothing appears in the Label I have set up.. Could someone direct me here?
You can just reset all the 3 buttons to their default state when any of the button is tapped and then highlight the current button.
#IBAction func buttonClicked(sender: UIButton) {
/** Reset all button to default state*/
resetAllButtons()
/** Highlight current button */
sender.backgroundColor = UIColor.redColor()
sender.titleColor = UIColor.whiteColor()
}
private func resetAllButtons() {
button1.backgroundColor = UIColor.clearColor()
button1.titleColor = UIColor.blackColor() /// Or use this : button1.setTitleColor(UIColor.blackColor, forState: .Normal)
button2.backgroundColor = UIColor.clearColor()
button2.titleColor = UIColor.blackColor()
button3.backgroundColor = UIColor.clearColor()
button3.titleColor = UIColor.blackColor()
}