2017-06-22 2 views
0

私は4つのサブビュー(label1、label2、label3、label4)と制約(label2 bottomがlabel1に依存する)を特定のビューにプログラマチックに追加しました。次に、insertSubiewAtIndexメソッドを使用して、別のサブビューをインデックス1(label5)に挿入します。制約が動的なのでUIをリフレッシュする方法は、label5はlabel1の位置をとり、label1はlabel2の位置をとるようにします。ここでスウィフトメソッドinsertSubview at indexはうまくいきます。しかし、挿入されたもののためのスペースを作るためにすべてのサブビューを再配置する方法

は私のコードは、私が実際に達成したい何

override func viewDidLoad() { 
    super.viewDidLoad() 

    let label1 = UILabel() 
    label1.translatesAutoresizingMaskIntoConstraints = false 
    label1.text = "label1" 
    label1.numberOfLines = 0 
    label1.tag = 1 

    self.containerView.addSubview(label1) 

    NSLayoutConstraint(item: label1, attribute: .top, relatedBy: .equal, toItem: self.containerView, attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true 

    NSLayoutConstraint(item: label1, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

    NSLayoutConstraint(item: label1, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true 


    let label2 = UILabel() 
    label2.translatesAutoresizingMaskIntoConstraints = false 
    label2.text = "label2" 
    label2.numberOfLines = 0 
    label2.tag = 1 

    self.containerView.addSubview(label2) 

    NSLayoutConstraint(item: label2, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true 

    NSLayoutConstraint(item: label2, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

    NSLayoutConstraint(item: label2, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true 


    let label3 = UILabel() 
    label3.translatesAutoresizingMaskIntoConstraints = false 
    label3.text = "label3" 
    label3.numberOfLines = 0 
    label3.tag = 1 

    self.containerView.addSubview(label3) 

    NSLayoutConstraint(item: label3, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true 

    NSLayoutConstraint(item: label3, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

    NSLayoutConstraint(item: label3, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true 


    let label4 = UILabel() 
    label4.translatesAutoresizingMaskIntoConstraints = false 
    label4.text = "label4" 
    label4.numberOfLines = 0 
    label4.tag = 1 

    self.containerView.addSubview(label4) 

    NSLayoutConstraint(item: label4, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true 

    NSLayoutConstraint(item: label4, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

    NSLayoutConstraint(item: label4, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

    let label5 = UILabel() 
    label5.translatesAutoresizingMaskIntoConstraints = false 
    label5.text = "label5" 
    label5.numberOfLines = 0 
    label5.tag = 1 

    self.containerView.addSubview(label5) 

    NSLayoutConstraint(item: label5, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[0], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true 

    NSLayoutConstraint(item: label5, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

    NSLayoutConstraint(item: label5, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true 

} 

答えて

0
override func viewDidLoad() { 
     super.viewDidLoad() 

     containerView.backgroundColor = .white 
     let label1 = UILabel() 
     label1.translatesAutoresizingMaskIntoConstraints = false 
     label1.text = "label1" 
     label1.numberOfLines = 0 
     label1.tag = 1 

     self.containerView.addSubview(label1) 

     let label2 = UILabel() 
     label2.translatesAutoresizingMaskIntoConstraints = false 
     label2.text = "label2" 
     label2.numberOfLines = 0 
     label2.tag = 1 

     self.containerView.addSubview(label2) 

     let label3 = UILabel() 
     label3.translatesAutoresizingMaskIntoConstraints = false 
     label3.text = "label3" 
     label3.numberOfLines = 0 
     label3.tag = 1 

     self.containerView.addSubview(label3) 

     let label4 = UILabel() 
     label4.translatesAutoresizingMaskIntoConstraints = false 
     label4.text = "label4" 
     label4.numberOfLines = 0 
     label4.tag = 1 

     self.containerView.addSubview(label4) 

     let label5 = UILabel() 
     label5.translatesAutoresizingMaskIntoConstraints = false 
     label5.text = "label5" 
     label5.numberOfLines = 0 
     label5.tag = 1 

     self.containerView.addSubview(label5) 

     setConstraint(to: [label5, label1, label2, label3, label4]) 
    } 

    private func setConstraint(to views: [UIView]) { 
     var iterator = views.makeIterator() 
     var prev: UIView = self.containerView 
     while let next = iterator.next() { 
      NSLayoutConstraint(item: next, attribute: .top, relatedBy: .equal, toItem: prev, attribute: .top, multiplier: 1, constant: 16).isActive = true 
      NSLayoutConstraint(item: next, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true 

      NSLayoutConstraint(item: next, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true 
      prev = next 
     } 
    } 
+0

である - 私は、任意のイベントで、親ビューの特定のインデックスのビューを挿入します。親ビューは既にロードされており、制約付きのビューが多数あります。イベントが同じ親ビュー内で切り替わるとします。私はスイッチをクリックした後、インデックスにビューを挿入したい場合は、スイッチをオフにして追加ビューを削除する必要があります。私の問題は、すべての制約が既に存在し、さまざまな種類のビューが多数あることです。すべての制約は、self.containerView.subviews [self.containerView.subviews.count - 2]のように動的です。制約を取り除いて再度追加する必要がありますか? – ankiraw91

+0

はい。 contentView.removeConstraints(contentView.constraints)次に新しい制約を追加します。 – linqingmo

関連する問題