为什么视图B更改时,具有底部约束的视图A不会移动

时间:2016-12-09 04:42:45

标签: ios swift

我在UIView内有一个按钮。设置底部约束。一旦我改变UIView的高度,我希望Button移动,因为它具有(相等)约束到底部。

enter image description here

设置底部约束:

enter image description here

嵌套在UIView内的按钮。

enter image description here

移动UITableView后,会调用以下代码:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let offset = -scrollView.contentOffset.y
    if offset > 100 {
        bgView.frame.size.height = offset
        bgView.layoutIfNeeded() // not a solution
        getBtn.layoutIfNeeded() // not a solution
    }

    view.bringSubview(toFront: tableView)
    if scrollView.contentOffset.y == -100.0 {
        view.bringSubview(toFront: bgView)
    }
}

按钮保持其位置。

enter image description here enter image description here

我错过了什么?非常感谢帮助。

根据要求:

enter image description here

1 个答案:

答案 0 :(得分:1)

为bgView高度约束创建一个IBOutlet,然后更改其常量

@IBOutlet weak var bgViewHeightConstraint: NSLayoutConstraint!

func scrollViewDidScroll(_ scrollView: UIScrollView) {

  let offset = -scrollView.contentOffset.y
  if offset > 100 {
     bgViewHeightConstraint.constant = offset
    view.layoutIfNeeded() 
  }
  ...
}