【问题标题】:How can I shift my label up and down on UIswitch on and off?如何在 UIswitch 打开和关闭时上下移动标签?
【发布时间】:2018-07-31 07:54:22
【问题描述】:

我有一个 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

【问题讨论】:

  • 为此目的使用 UIStackView。
  • @Vishal16 如果您可以提供一个简短的工作代码,那将很容易理解。 :)
  • 您是在使用情节提要还是以编程方式添加视图?
  • 以编程方式。我不使用故事板
  • 检查我的答案,请点赞并接受。如果它可以帮助你。

标签: ios swift autolayout uiswitch


【解决方案1】:

它可以帮助你。请遵循以下几点:

  • 在情节提要或 XIB 中将 UIScrollView 添加到您的 UIViewController
  • 启动一个NSMutableArray 命名它arrViews 获取服务器响应并在数组中添加视图。
  • 在 init 方法中初始化 UIStackViewpass 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
    }
}

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

【讨论】:

  • 我不需要为视图添加约束吗?没有得到这个让 arrViews = createSubViews(view)
  • 您在使用 XIB 吗?因此,您可以为该 xib 的子视图分配约束,如果其间距相等,则无需为单个视图分配约束。
  • 我在上面添加了一张图片。请参考该图像。当所需燃料的开关打开时,视图应显示在其下方,切换为超载视图及其下方的开关。
  • 这样您就可以让 3 个视图添加一个标签并切换为其他两个视图的子视图(需要燃料或超载),还可以以编程方式向子视图添加约束,然后将所有 3 个视图添加到 UIstackView。就是这样。
  • 第一个视图包含燃料标签和开关作为子视图。第二个视图包含重载的标签和开关作为子视图。第三种观点是什么?以及这些视图的约束条件如何?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2011-09-04
相关资源
最近更新 更多