UIView以编程方式获取和设置约束

时间:2015-03-19 16:34:53

标签: objective-c swift

如何获取当前约束并稍后为UIView设置它们。我这样做了吗?

当前尝试:

获取当前限制

 // get current constraints
 var txtViewConstraints = txtView.constraints()

稍后设置约束

override func viewDidLayoutSubviews() {
    if txtViewConstraints.isEmpty == false {
        txtView.addConstraints(txtViewConstraints)
    }
}

编辑:这是此问题的后续内容:BringSubViewToFront Repositions UIView in AutoLayout

更新代码用于移动txtView:

@IBAction func moveTxtView(sender: UIPanGestureRecognizer) {
        var translation: CGPoint =  sender.translationInView(self.view)
        sender.view?.center = CGPointMake(sender.view!.center.x +  translation.x, sender.view!.center.y + translation.y)
        sender.setTranslation(CGPointMake(0, 0), inView: self.view)

        txtViewConstraints = txtView.constraints()
        txtViewPosition = CGRect(x: txtView.frame.origin.x, y: txtView.frame.origin.y, width: txtView.frame.size.width, height: txtView.frame.size.height)
    }

0 个答案:

没有答案