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

How can I shift my label up and down on UIswitch on and off?

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

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

image

可能对你有帮助。请遵循以下几点:

  • UIScrollView 添加到故事板或 XIB 中的 UIViewController
  • 启动一个 NSMutableArray 命名它 arrViews 获取服务器响应并在数组中添加视图。
  • 初始化 UIStackView在 init 方法中传递 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
    }
}

希望对您有所帮助。编码愉快:)