如何在UIswitch打开和关闭时上下移动标签



我有一个UI开关,当它打开时,我想在它下面显示一个视图,并向下移动所有其他视图。类似地,当开关关闭时,视图应该消失,并像以前一样重新排列所有视图。

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

图像

它可能会对您有所帮助。请遵循以下几点:

  • UIScrollView添加到情节提要或XIB中的UIViewController
  • 启动一个NSMutableArray名称,arrViews获得服务器响应并在阵列中添加视图
  • 初始化init方法中的UIStackView传递arrView数组
  • 之后,UIStackView将被添加到UIScrollView的子视图中
  • 以编程方式向UIStackView添加约束。就是这样。

    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
    

之后,当您单击开关时,从视图阵列中获取一个视图并将其隐藏,所有其他视图都会自动重新排列。

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

希望它能帮助你。快乐编码:(

最新更新