Reputation: 105
I'm new in creating views without interface builder .
I'm using NSLayoutAnchors
to create views!
when I'm using some view that created in viewcontroller like this :
let borderView:UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.lightGray
view.isUserInteractionEnabled = false
view.alpha = 0.5
return view
}()
then i use this view to border sth in view like this :
view.addSubview(borderView)
borderView.bottomAnchor.constraint(equalTo: headerView.bottomAnchor).isActive = true
borderView.leftAnchor.constraint(equalTo: headerView.leftAnchor).isActive = true
borderView.rightAnchor.constraint(equalTo: headerView.rightAnchor).isActive = true
borderView.heightAnchor.constraint(equalToConstant: 1).isActive = true
then in another view i try this :
informationView.addSubview(borderView)
borderView.topAnchor.constraint(equalTo: informationView.topAnchor).isActive = true
borderView.leftAnchor.constraint(equalTo: informationView.leftAnchor).isActive = true
borderView.rightAnchor.constraint(equalTo: informationView.rightAnchor).isActive = true
borderView.heightAnchor.constraint(equalToConstant: 1).isActive = true
but looks like this view has it's previous constraints end show constraints error !
how can I remove borderView
constraints before reusing it ?
Upvotes: 2
Views: 5099
Reputation: 89142
You can't reuse views. Each view can only be in the view hierarchy one time. You need to make a new bordered view object for each view you want to use it with.
To answer the question, you can remove constraints from a view with view.removeConstraints(view.constraints)
Upvotes: 7