Hello!
Having a go at swipe-to-delete functions and the likes in tableView cells today...
When toggling a button I would like it to change colours from green to red and back again. - Any suggestions on how to achieve this?
For the toggle button I have declared a boolean which is set
to false at start.
and this is what I've got inside editActionsForRowAtIndexPath:
...
let toggleAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Toggle", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let button: UIButton = UIButton()
button.backgroundColor = UIColor.greenColor()
if(self.toggleBool == false) {
self.toggleBool = true
button.backgroundColor = UIColor.redColor()
} else {
self.toggleBool = false
toggleButton.backgroundColor = UIColor.greenColor()
}
self.view.addSubview(button)
})
return [deleteAction, toggleAction]
}
The code snippet above does not make the button change colour when pressed!
Any help in the matter would be much appreciated!
Related
I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.
I would like to titleColor of my UIBUtton to change when the user taps on the button, with the below code currently the color of the UIBUtton title changes if the user press and hold on the button, however, not when tapped quickly, could you please let me know what do I need to change in the below code to make the titleColor change upon a tap detection only:
var resetFiltersButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Clear Filters", for: .normal)
button.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Bold", size: 20)
button.setTitleColor(.blue, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.titleLabel?.numberOfLines = 1
button.tag = 1
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc func buttonPressed(_ sender: UIButton) {
// MARK: - Clear Filters button pressed:
if sender.tag == 1 {
}
}
Why don't you try implementing that inside the buttonPressed function?
#objc func buttonPressed(_ sender: UIButton) {
sender.setTitleColor(color: UIColorUI, for: UIControl.State)
}
This issue is that you are using setTitleColor for .normal and .highlighted states.
.normal is when the button is not clicked.
.highlighted is while the button is being clicked on.
Instead you could:
Just updated the .normal color when pressed (in buttonPressed); or
setTitleColor for .selected for after it's clicked and handle the selection state within buttonPressed (add sender.isSelected = !sender.IsSelected)
i have been coding for a while i am stuck with tableview single selection/deselection.
I have a tableview with custom cell which has a button. if i click on one button another button in bottom gets selected as well.For example when i click on button with index 2 another button gets click in the button.
it should be like when i click on one button other buttons should be deselected.
Thanks!
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected == true {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.isSelected = false
}
else {
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
cell.EventReviewQuickeReviewBtn.isSelected = true
}
}
Cells get reused. You need to ensure the exact same set of properties are set/reset in both conditions.
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.white // or whatever
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.red, for: .normal) // or whatever
} else {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.black // or whatever
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.green // or whatever
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 3 // or whatever
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
}
cell.EventReviewQuickeReviewBtn.isSelected = !cell.EventReviewQuickeReviewBtn.isSelected
}
when you select any button then you should have mark isSelected property to false
assuming you have a list of models for your table view like
class Example{
...
var isSelected: Bool
...
}
(in ViewController)
let list: [Example] = [example1, example2, example3]
on tap of any button deselect all then selected desire button like
list.forEach { (example) in
example.isSelected = false
}
list[selectedButtonIndex].isSelected = true
and then reload your tableView.
I know how to add tags in main.storyboard files, but now I am working on creating everything programmatically. However, I couldn't find anything online that tells me how to add tags to a button. I tried to read the apple documentation but don't know how to implement it.
Here is for example how I created my button, how can I add a tag to it, so that when I make multiple buttons and want to link them to a single action I can know what button has been pressed?
let buttonOne() : UIButton {
let button = UIButton(type: .system)
button.backgroundColor = .red
return button
}()
Try this:
let buttonOne() : UIButton {
let button = UIButton(type: .system)
button.backgroundColor = .red
button.tag = 2
return button
}()
Declare The Button
let menuChooseButton = UIButton()
menuChooseButton.frame.origin.y = 0
menuChooseButton.frame.origin.x = xPosition
menuChooseButton.frame.size.width = subViewWidth
menuChooseButton.frame.size.height = subViewHeight
menuChooseButton.backgroundColor = .clear
menuChooseButton.setTitle("", for: .normal)
menuChooseButton.tag = i
menuChooseButton.addTarget(self, action: #selector(menuSelectAction), for: .touchUpInside)
menuScrollView.addSubview(menuChooseButton)
Define Button Action
#objc func menuSelectAction(sender: UIButton!){
var tagNo: Int = sender.tag
if tagNo == 0{
// Whatever you want
}
}
I have a view in my tabbar controller where I would like to show a button. I create this button programmatically based of a condition, therefore I use the following code but nothing is appearing:
override func viewDidLoad() {
super.viewDidLoad()
if !Settings.getIsConnected() {
notConnected()
}
}
func notConnected() {
let connectBtn = UIButton(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: 200, height: 45))
connectBtn.setTitle("Connect", forState: .Normal)
connectBtn.addTarget(self, action:#selector(self.pressedConnect(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(connectBtn)
print("Button created")
}
func pressedConnect(sender: UIButton!) {
}
I am clueless on what I am doing wrong. Anyone got suggestions? Cause it does print out "Button created" so it definitely runs the code inside the noConnected() method.
Add a background color to your UIButton and add a tint color to the title. This will resolve the problem
Try moving the code to viewDidAppear and see if the button is showing up.
The frame is not correctly set when in viewDidLoad. Use the method viewDidLayoutSubviews for the earliest possible time where the frame is correctly setup for a ViewController.
With this code change, you will need some additional logic for when your button should be added as a subview though.
A programmatically created button may not show up because of more reasons, e.g:
the tint color is not set
the background color is not set
the button is not added to the view hierarchy
the button is hidden
In your case, you should change the tint color or the background color of your button.
E.g.:
Swift 4.2:
private lazy var connectButton: UIButton = {
let button = UIButton(type: .custom)
button.backgroundColor = .green
button.setTitleColor(.black, for: .normal)
button.setTitle(NSLocalizedString("Connect", comment: ""), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(connectButton)
}
You can re-check the button properties in the storyboard that it is not hidden.