【问题标题】:IOS swift expandable view [closed]IOS swift可扩展视图[关闭]
【发布时间】:2014-07-29 11:31:55
【问题描述】:

我想在打开开关时展开视图,并在关闭开关时折叠视图。 同时,我设计中的所有其他元素都必须向下或向上移动。

以下是开关关闭

时的布局

下面是开关ON时布局的样子:

我希望你明白我想要做什么。

还有一个问题……我该怎么做???

【问题讨论】:

  • 是的,我明白你想要做什么。那么问题是什么?
  • 问题...我该怎么做?
  • 一种方法是使用 UITableView 并在更改开关时动态插入/删除行(或仅 reloadData)。一个例子:stackoverflow.com/questions/9855972/…

标签: ios swift expandable


【解决方案1】:

使用 AutoLayout 和 InterfaceBuilder 内部(使用 xib 或故事板)要容易得多,因此如果需要,首先要熟悉它们。

  1. 在 UISwitches 周围包裹一个容器 UIView 并确保将其设置为 Clip Subviews。
  2. 为容器视图的高度添加约束并将其设置为 0。
  3. 将该约束拖到您的类声明中以生成 IBOutlet 属性。
  4. 从您的 UISwitch 'Value Changed' 事件中将一个新的插座拖到类中以生成 IBAction 方法。
  5. 确保实现该方法,以便在值更改时,根据您的更改为高度约束(IBOutlet)切换动画

    self.firstHeightConstraint.constant = on ? 200.0 : 0.0
    self.view.setNeedsUpdateConstraints()
    UIView.animateWithDuration( ... 
        options:.BeginFromCurrentState, 
        animations: { self.view.layoutIfNeeded() } ...
    )
    

【讨论】:

    【解决方案2】:

    使用 UIView,这不是一个好方法。您应该使用 UITableView 而不是视图。

    使用 TableView,您可以根据第一张图片在 UITableView 的标题中添加带有标题的 UISwitch,并在状态为 ON 时返回 TableView 更改部分中所需的行数,在状态为 OFF 时返回行数 0。

    还有问题,请告诉我,我会帮你的。

    【讨论】:

      【解决方案3】:

      我知道我的代码不是最干净的,但是当答案告诉提问者它不是一个好方法或这样做时,它会困扰我。我提供了一个不使用表视图或约束的解决方案:

      在您的示例中,您可以为需要移动的每个“级别”使用标签。因此,从开关 2 及其下方的所有视图开始,您将拥有该级别的标签(第二个隐藏的可折叠视图也将具有标签 2),然后开关 3 将具有标签 3。

      对于隐藏的标签,您将它们添加到我称为 insideView1 或 insideView2 的子视图中。这些内部视图的框架应具有适当的 minX、minY 和宽度,但开始时的高度为 0。同样重要的是,它们有 layer.masksToBounds = false,因此当它们的高度为 0 时,它们的子视图不会显示。然后我们将高度更改为适当的高度(扩展),以及更改位置下的所有子视图。

      最后我制作了一个字典来跟踪一个部分何时展开,这样您就可以使用相同的功能来展开/折叠已经展开的其他区域。

      import UIKit
      
      class ExpandViewController: UIViewController {
      
          var insideView1 = UIView()
          var insideView2 = UIView()
          var expand = [1:true,2:true]
          
          @objc func expandCollapse(_ sender : UIButton) {
              var heightChange = CGFloat(0)
              switch sender.tag {
              case 1:
                  heightChange = 90
              default:
                  heightChange = 90
              }
      
              var viewsToExpand = [UIView(),insideView1,insideView2]
              
              UIView.animate(withDuration: 1.0) { [self] in
                  let expandMe = viewsToExpand[sender.tag]
                  if expand[sender.tag] == true {
                      expandMe.frame.size.height = heightChange
                      for v in view.subviews {
                          if v.tag > sender.tag {
                              v.center.y += heightChange
                          }
                      }
                  } else {
                      expandMe.frame.size.height = 0
                      for v in view.subviews {
                          if v.tag > sender.tag {
                              v.center.y -= heightChange
                          }
                      }
                  }
                  expand[sender.tag] = !expand[sender.tag]!
              }
          }
          
          
          override func viewDidLoad() {
              super.viewDidLoad()
           
              let switcher1 = UISwitch()
              switcher1.tag = 1
              switcher1.isOn = false
              switcher1.frame = CGRect(x: view.bounds.width - 150, y: 100, width: 40, height: 30)
              switcher1.addTarget(self, action: #selector(expandCollapse(_:)), for: .allTouchEvents)
              view.addSubview(switcher1)
              let switchLabel1 = UILabel()
              switchLabel1.tag = 1
              switchLabel1.text = "Switch 1"
              switchLabel1.frame = CGRect(x: 30, y: 0, width: view.bounds.width, height: 30)
              switchLabel1.center.y = switcher1.center.y
              view.addSubview(switchLabel1)
              
              let switcher2 = UISwitch()
              switcher2.tag = 2
              switcher2.isOn = false
              switcher2.frame = CGRect(x: view.bounds.width - 150, y: 140, width: 40, height: 30)
              switcher2.addTarget(self, action: #selector(expandCollapse(_:)), for: .allTouchEvents)
              view.addSubview(switcher2)
              let switchLabel2 = UILabel()
              switchLabel2.tag = 2
              switchLabel2.text = "Switch 2"
              switchLabel2.frame = CGRect(x: 30, y: 0, width: view.bounds.width, height: 30)
              switchLabel2.center.y = switcher2.center.y
              view.addSubview(switchLabel2)
              
              let switcher3 = UISwitch()
              switcher3.tag = 3
              switcher3.isOn = false
              switcher3.frame = CGRect(x: view.bounds.width - 150, y: 180, width: 40, height: 30)
      //        switcher3.addTarget(self, action: #selector(expandCollapse(_:)), for: .allTouchEvents)
              view.addSubview(switcher3)
              let switchLabel3 = UILabel()
              switchLabel3.tag = 3
              switchLabel3.text = "Switch 3"
              switchLabel3.frame = CGRect(x: 30, y: 0, width: view.bounds.width, height: 30)
              switchLabel3.center.y = switcher3.center.y
              view.addSubview(switchLabel3)
              
              insideView1.frame = CGRect(x: 0, y: switcher1.frame.maxY + 5, width: view.bounds.width, height: 0)
              insideView1.layer.backgroundColor = UIColor.lightGray.cgColor
              insideView1.layer.masksToBounds = true
              insideView1.tag = 1
              view.addSubview(insideView1)
              
              insideView2.frame = CGRect(x: 0, y: switcher2.frame.maxY + 5, width: view.bounds.width, height: 0)
              insideView2.layer.backgroundColor = UIColor.lightGray.cgColor
              insideView2.layer.masksToBounds = true
              insideView2.tag = 2
              view.addSubview(insideView2)
              
              let insideLabel1 = UILabel()
              insideLabel1.text = "Label"
              insideLabel1.frame = CGRect(x: 60, y: 10, width: view.bounds.width, height: 30)
              insideView1.addSubview(insideLabel1)
              let insideTextField1 = UITextField()
              insideTextField1.frame = CGRect(x: 200, y: 0, width: view.bounds.width - 240, height: 30)
              insideTextField1.center.y = insideLabel1.center.y
              insideTextField1.layer.backgroundColor = UIColor.white.cgColor
              insideView1.addSubview(insideTextField1)
              
              let insideLabel2 = UILabel()
              insideLabel2.text = "Label"
              insideLabel2.frame = CGRect(x: 60, y: 50, width: view.bounds.width, height: 30)
              insideView1.addSubview(insideLabel2)
              let insideTextField2 = UITextField()
              insideTextField2.frame = CGRect(x: 200, y: 0, width: view.bounds.width - 240, height: 30)
              insideTextField2.center.y = insideLabel2.center.y
              insideTextField2.layer.backgroundColor = UIColor.white.cgColor
              insideView1.addSubview(insideTextField2)
              
              let insideLabel3 = UILabel()
              insideLabel3.text = "Label"
              insideLabel3.frame = CGRect(x: 60, y: 10, width: view.bounds.width, height: 30)
              insideView2.addSubview(insideLabel3)
              let insideTextField3 = UITextField()
              insideTextField3.frame = CGRect(x: 200, y: 0, width: view.bounds.width - 240, height: 30)
              insideTextField3.center.y = insideLabel3.center.y
              insideTextField3.layer.backgroundColor = UIColor.white.cgColor
              insideView2.addSubview(insideTextField3)
              
              let insideLabel4 = UILabel()
              insideLabel4.text = "Label"
              insideLabel4.frame = CGRect(x: 60, y: 50, width: view.bounds.width, height: 30)
              insideView2.addSubview(insideLabel4)
              let insideTextField4 = UITextField()
              insideTextField4.frame = CGRect(x: 200, y: 0, width: view.bounds.width - 240, height: 30)
              insideTextField4.center.y = insideLabel4.center.y
              insideTextField4.layer.backgroundColor = UIColor.white.cgColor
              insideView2.addSubview(insideTextField4)
          }
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-24
        • 2021-08-12
        • 2013-04-02
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多