Both button background color changed - ios

When I'm using that function, the button not correctly worked. When I tapped on button then the button background color will change and when we tap on another button, then the background color of both the button will change. Please tell me how to solve this bug.
#IBAction func btnNew(_ sender: Any)
{
if otlNewVisitor.isSelected == false
{
otlNewVisitor.isSelected == true
Button.buttonPressed(button: otlNewVisitor, boolResult: true, titleColor: UIColor.white, strImage: "icn-new-visitor-wht", bgColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), imgVW: imgNewVis)
//Entered SubmitDetails Screen
let submitVC = self.storyboard?.instantiateViewController(withIdentifier: "SubmitDetailsVC") as! SubmitDetailsVC
self.navigationController?.pushViewController(submitVC, animated: true)
}
else
{
otlNewVisitor.isSelected == false
//Button Colow did change
Button.buttonPressed(button: otlNewVisitor, boolResult: false, titleColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), strImage: "icn-new-visitor", bgColor: UIColor.white, imgVW: imgNewVis)
}
}

You can changed your code to my code. This is only happens when you set the selected value of button within the if-else condition. So, you want to remove that code. It works well.
#IBAction func btnNew(_ sender: Any)
{
if otlNewVisitor.isSelected == false
{
Button.buttonPressed(button: otlNewVisitor, boolResult: true, titleColor: UIColor.white, strImage: "icn-new-visitor-wht", bgColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), imgVW: imgNewVis)
//Entered SubmitDetails Screen
let submitVC = self.storyboard?.instantiateViewController(withIdentifier: "SubmitDetailsVC") as! SubmitDetailsVC
self.navigationController?.pushViewController(submitVC, animated: true)
}
else
{
//Button Colow did change
Button.buttonPressed(button: otlNewVisitor, boolResult: false, titleColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), strImage: "icn-new-visitor", bgColor: UIColor.white, imgVW: imgNewVis)
}
}

Related

UINavigationBar tintColor is misbehaving

I have issue when set UINavigationBar tintColor.
It is misbehaving
I'm using xcode 11.3.1, swift 5, iOS 13.3
*MyClass
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "test"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
}
*result
real device
https://imgur.com/sONr4vq
simulator
https://imgur.com/vs5lhgR
I want to set back button color like title
I only get error on real device
Please help me
Thank you
To set back button and hide "back":
let backItem = UIBarButtonItem()
backItem.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
navigationItem.backBarButtonItem = backItem
You should but this code to the ViewController before push the new one.
Try access to navigationController.navigationBar instead of navigationController.view :
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Did you try using UIColor instead of #colorLiteral?
Like
self.navigationController?.navigationBar.tintColor = UIColor(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
//or
self.navigationController?.navigationBar.tintColor = UIColor(displayP3Red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Edit:
Since that the above did not work for you, did you try this?
self.navigationController?.navigationBar.tintColor = self.navigationController?.navigationItem.titleView?.backgroundColor
Considering that all you want is to have both the button and title to have the same colour right?

Swift create a common extension for UIButton and UIView Color change

In my case, I am trying to creating multiple buttons. Here, each buttons placed on separate UIView. This buttons working like a single section based on selection Its title color and UIView color I am changing in each button acton method. Here, I need to create a common extension for all button title and UIView color change. Once, button click need to pass the value to extension or a function to change the colors for selection button. This is I am trying for reducing the code duplication and LOC.
NOTE: Below I posted only one button code but I have many button. I want to make it common class and pass the value to change the colors. How to achieve this?
First Button Action
#IBAction func firstButtonClick(_ sender: Any) {
self.onetimeView.backgroundColor = colorLiteral(red: 0.184337255, green: 0.683529412, blue: 0.976475882, alpha: 1)
self.dailyView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.weeklyView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.fiftydaysView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.monthlyView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.onetimeButton.setTitleColor(UIColor.selectedColor, for: .normal)
self.dailyButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.weeklyButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.fiftydaysButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.monthlyButton.setTitleColor(UIColor.disabledColor, for: .normal)
}
#IBAction func secondButtonClick(_ sender: Any) {
self.onetimeView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.dailyView.backgroundColor = colorLiteral(red: 0.184337255, green: 0.683529412, blue: 0.976475882, alpha: 1)
self.weeklyView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.fiftydaysView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.monthlyView.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.onetimeButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.dailyButton.setTitleColor(UIColor.selectedColor, for: .normal)
self.weeklyButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.fiftydaysButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.monthlyButton.setTitleColor(UIColor.disabledColor, for: .normal)
}
extension UIColor {
static var selectedColor = UIColor.init(red: 47/255, green: 174/255, blue: 248/255, alpha: 1)
static var disabledColor = UIColor.init(red: 170/255, green: 170/255, blue: 170/255, alpha: 1)
}
You can create a subclass like:
class PrimaryButton: UIButton {
}
Store your buttons and views in some data structure and iterate over it. Create a function that will set selectedColor on views passed as an argument and disabledColor on the rest.
typealias Section = (UIButton, UIView)
let sections: [Section] = [
(button: onetimeButton, view: onetimeView),
(button: dailyButton, view: dailyView),
(button: weeklyButton, view: weeklyView),
(button: fiftydaysButton, view: fiftydaysView),
(button: monthlyButton, view: monthlyView),
]
func select(_ section: Section) {
sections.forEach { (section) in
section.0.setTitleColor(UIColor.disabledColor, for: .normal)
section.1.backgroundColor = UIColor.disabledColor
}
section.0.setTitleColor(UIColor.selectedColor, for: .normal)
section.1.backgroundColor = UIColor.selectedColor
}
// in UIButton click call
select((onetimeButton, onetimeView))

How to hide elements in a Stack View

I have 4 separate views and I want to hide the other 3 of them when one of the buttons is pressed.
I have them in a UIStackView but .isHidden = true does not hide the views for some reason.
It works fine when they're not in a stack view.
#IBAction func qbpressed(_ sender: Any) {
QBContainer.isHidden = false
WRContainer.isHidden = true
RBContainer.isHidden = true
QBIndicator.isHidden = false
WRIndicator.isHidden = true
RBIndicator.isHidden = true
TEIndicator.isHidden = true
QBButton.setTitleColor(#colorLiteral(red: 0, green: 0.5008062124, blue: 1, alpha: 1), for: .normal)
WRButton.setTitleColor(#colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), for: .normal)
RBButton.setTitleColor(#colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), for: .normal)
TEButton.setTitleColor(#colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), for: .normal)
if intersitial.isReady{
intersitial.present(fromRootViewController: self)
}
}
isHidden property doesn't work, but you can use alpha and achieve the same result,
QBIndicator.alpha = 1.0 will work for QBIndicator.isHidden = false and
QBIndicator.alpha = 0.0 will work for QBIndicator.isHidden = true
setting a view to hidden should make it no longer visible, regardless of whether or not it inside a UIStackView.
The benefit of UIStackView is that it provides free animation through the isHidden property, like so:
// Assuming stackViewSubView.isHidden == false here
UIView.animate(withDuration: 0.25, animations: {
self.stackViewSubView.isHidden = true
self.view.layoutIfNeeded()
})

How To Store Data Permanently With User Defaults In xCode Swift for toggle button

How can I save the state of my toggle button using userdefaults? Here is the code, please help me.
import UIKit
class LawFigure: UIViewController {
//Outlets:
#IBOutlet weak var likeButton: UIButton!
//Variables:
var isButtonOn = false
override func viewDidLoad() {
super.viewDidLoad()
customiseUI()
}
func customiseUI() {
likeButton.layer.borderWidth = 3.0
likeButton.layer.borderColor = UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0).cgColor
likeButton.layer.cornerRadius = 20.0
likeButton.clipsToBounds = true
}
#IBAction func followActionPressed(_ sender: Any) {
self.activateButton(bool: !isButtonOn)
}
func activateButton(bool: Bool) {
isButtonOn = bool
likeButton.backgroundColor = bool ? UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0) : .clear
likeButton.setTitle(bool ? "unlike": "like", for: .normal)
likeButton.setTitleColor(bool ? .white : UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0), for: .normal)
}
}
You can try every open of the app
likeButton.isEnabled = !(UserDefaults.standard.bool(forKey:"IsBtnEnabled"))
whenever button is clicked save it
UserDefaults.standard.set(state, forKey: "IsBtnEnabled")
var isButtonOn = UserDefaults.standard.bool(forKey: "isButtonOn")
override func viewDidLoad() {
super.viewDidLoad()
activateButton(bool: isButtonOn)
customiseUI()
}
func activateButton(bool: Bool) {
isButtonOn = bool
UserDefaults.standard.set(isButtonOn, forKey: "isButtonOn")
UserDefaults.standard.synchronize()
likeButton.backgroundColor = bool ? UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0) : .clear
likeButton.setTitle(bool ? "unlike": "like", for: .normal)
likeButton.setTitleColor(bool ? .white : UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0), for: .normal)
}

UIButton not working loaded in a child ViewController

I've got a MainViewController where I'm loading a slideout-menu. The MainViewController contains a static TopView and a ContentView where different child controllers or views are loaded, depending on which menu entry was selected.
There is a button inside one of the children is loaded, but it is not working.
When I start the app with the ViewController including the button, set to "is initial View Controller", everything works properly.
When starting the app with the MainViewController as initial View Controller with a loaded child, the button is not working (it is loaded in the view, but not reacting).
Any suggestions how I can make this work?
MainViewController loading of Child View
class MainViewController: UIViewController, SideBarDelegate {
var contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
....
contentView.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
contentView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(contentView)
contentView.topAnchor.constraint(equalTo: statusBarView.bottomAnchor).isActive = true
contentView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
contentView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
contentView.layoutIfNeeded()
}
func sideBarDidSelectButtonAtIndex(_ index: Int) {
...
if index == 0{
statusBarLabel.text = "First"
let controller:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
controller.view.frame = ContentView.bounds
controller.willMove(toParentViewController: self)
contentView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
print("touched index 0")
} else if index == 1{
// index is 1
}
}
FirstViewController
class FirstViewController: UIViewController, UIScrollViewDelegate {
let addButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
addButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
addButton.clipsToBounds = true
addButton.setTitleColor(UIColor.white, for: .normal)
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
addButton.setTitle("add feed", for: .normal)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightRegular)
self.view.addSubview(AddButton)
addButton.layoutIfNeeded()
addButton.addTarget(self, action: #selector(touchUpAddButton), for: UIControlEvents.touchUpInside)
addButton.addTarget(self, action: #selector(touchDownAddButton), for: UIControlEvents.touchDown)
func touchUpAddButton() {
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
}
func touchDownAddButton() {
addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}
}
While adding the view in the container you don't have to use addSubview function, that the FirstViewController functionality is not working.
Use below code:
let newViewController:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
let oldViewController = childViewControllers.last! as UIViewController
oldViewController.willMove(toParentViewController: nil)
addChildViewController(newViewController)
transition(from: oldViewController, to: newViewController, duration: 0.25, options: .transitionCrossDissolve, animations:{ () -> Void in
// nothing needed here
}, completion: { (finished) -> Void in
oldViewController.removeFromParentViewController()
newViewController.didMove(toParentViewController: self)
})
You can change the duration, it used for animation
Update your functions to these
func TouchUpAddButton(sender: UIButton){
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
}
func TouchDownAddButton(sender: UIButton) {
addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}
And change your code of adding the target to
addButton.addTarget(self, action: #selector(FirstViewController.touchUpAddButton(_:)), for: .touchDown)
addButton.addTarget(self, action: #selector(FirstViewController.touchDownAddButton(_:)), for: .touchDown)
First solution:
Remove an autolayout constrains
Second solution:
Button is not performing an action because a child view controller view should be added directly to a container controller view (in your case: MainViewController view) and not via an intermediate view (in your case: contentView)

Resources