How to hide elements in a Stack View - ios

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()
})

Related

Color of label does not change when used in animation

#IBAction func firstButton(_ sender: UIButton) {
if textLabel.text == "Cat" {
textLabel.backgroundColor = .green
view.backgroundColor = .green
secondButtonOutlet.isHidden = false
firstButtonOutlet.isHidden = true
picker.isHidden = true
buttonBackOutlet.isHidden = false
} else {
secondButtonOutlet.isHidden = true
countMistakes += 1
view.backgroundColor = .systemRed
textLabel.backgroundColor = .systemRed
UIView.animate(withDuration: 0, delay: 1, options: .allowAnimatedContent, animations: {
() -> () in
self.view.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
self.textLabel.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
}, completion: nil)
}
}
I have this code. My goal is to make an animation of red screen blinking. Everything goes well until textLabel. It doesn't want to change its color at all. Can anyone help me how to fix it ? (Background color is changing, so the code should be correct in overall).

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))

Both button background color changed

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)
}
}

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)

View stuck behind MapView

I have tried everything to make my view appear over the map view (see code below), but it still won't show up. I don't know what to do. This is the mapView at 0.5 alpha
segmentedControl = DPSegmentedControl.init(
FrameWithoutIcon: CGRectMake(307.5, 566, 235, 37.5),
items: ["Standard", "Satellite", "Hybrid"],
backgroundColor: UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1),
thumbColor: UIColor.init(hex: "#B60002"),
textColor: UIColor(hex: "#808080"),
selectedTextColor: UIColor(hex: "#FFFFFF"))
segmentedControl.alpha = 0
segmentedControl.selectedIndex = 0
segmentedControl.addTarget(self, action: #selector(self.tapSegment(_:)), forControlEvents: .ValueChanged)
self.view.insertSubview(self.segmentedControl, belowSubview: self.hoverBar)
UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(self.segmentedControl)
UIApplication.sharedApplication().keyWindow!.sendSubviewToBack(self.mapView)
When I use z position, the view appears, but it does not allow any touch actions (it is a custom segmented control).
self.segmentedControl.layer.zPosition = self.mapView.layer.zPosition + 1
Have your tried either of these?
self.view.bringSubviewToFront(self.segmentedControl)
self.view.sendSubviewToBack(self.mapView)
or
self.segmentedControl?.superview.bringSubviewToFront(self.segmentedControl)
self.mapView?.superview.sendSubviewToBack(self.mapView)

Resources