I have a UIswitch and when it is on I want to present a view below it and shift all the other views down. Similarly when the switch is off , the view should disappear rearranging all the views as before.
@objc func switchValueDidChange(_ sender: UISwitch) {
if sender == fuelSwitch{
if (sender.isOn == true){
// show fuel view
print("on")
fuelRequiredView?.isHidden = false
topConstraint = layoutConstraints.setTopMargin(isOverloadedLabel, attribute1: .topMargin, relatedBy: .equal, toItem: fuelRequiredView, attribute2: .topMargin, multiplier: 2.0, constant: 0)
contentView?.addConstraint(topConstraint!)
topConstraint = layoutConstraints.setTopMargin(overloadedSwitch, attribute1: .topMargin, relatedBy: .equal, toItem: fuelRequiredView, attribute2: .topMargin, multiplier: 2.0, constant: 0)
contentView?.addConstraint(topConstraint!)
}
else{
// hide fuel view
print("off")
fuelRequiredView?.isHidden = true
topConstraint = layoutConstraints.setTopMargin(isOverloadedLabel, attribute1: .topMargin, relatedBy: .equal, toItem: contentView, attribute2: .topMargin, multiplier: 2.4, constant: 0)
contentView?.addConstraint(topConstraint!)
topConstraint = layoutConstraints.setTopMargin(overloadedSwitch, attribute1: .topMargin, relatedBy: .equal, toItem: contentView, attribute2: .topMargin, multiplier: 2.4, constant: 0)
contentView?.addConstraint(topConstraint!)
}
}
It may help you. Please follow this points:
UIScrollView
to your UIViewController
in storyboard or XIB.NSMutableArray
name it arrViews
gets server response and adds view in the array.UIStackView
pass arrView
array in the init method. UIStackView
will be added subview of UIScrollView
.Add constraint programmatically to UIStackView
. That's it.
let arrViews = createSubViews(view)
let stackView = UIStackView(arrangedSubviews: arrViews)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 16
stackView.distribution = .fill
self.scrollView.addSubview(stackView)
//constraints
let leading = NSLayoutConstraint(item: stackView, attribute: .leading, relatedBy: .equal, toItem: self.scrollView, attribute: .leading, multiplier: 1.0, constant: 0)
self.scrollView.addConstraint(leading)
let trailing = NSLayoutConstraint(item: stackView, attribute: .trailing, relatedBy: .equal, toItem: self.scrollView, attribute: .trailing, multiplier: 1.0, constant: 0)
self.scrollView.addConstraint(trailing)
let top = NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1.0, constant: 0)
self.scrollView.addConstraint(top)
let bottom = NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1.0, constant: 0)
self.scrollView.addConstraint(bottom)
let equalWidth = NSLayoutConstraint(item: stackView, attribute: .width, relatedBy: .equal, toItem: self.scrollView, attribute: .width, multiplier: 1.0, constant: 0)
self.scrollView.addConstraint(equalWidth)
leading.isActive = true
trailing.isActive = true
top.isActive = true
bottom.isActive = true
equalWidth.isActive = true
After that when you click on the switch get a view from an array of views and hide it all other views automatically rearranged.
@objc func switchValueDidChange(_ sender: UISwitch) {
if sender == fuelSwitch{
let fuelRequiredView = arrViews[index] // get index of fuelRequiredView
if (sender.isOn == true){
// show fuel view
print("on")
fuelRequiredView?.isHidden = false
}
else{
// hide fuel view
print("off")
fuelRequiredView?.isHidden = true
}
}
Hope it will help you. Happy coding :)