Programmatically creating constraints bound to view controller margins

前端 未结 3 1246
广开言路
广开言路 2020-12-08 21:32

I\'m trying to make a view that will act as a sort of \"panel\", attached to the right side of the view controller.

That is, it is bound to the trailing, top, and b

3条回答
  •  离开以前
    2020-12-08 21:43

    There seems to be some ambiguity in your code, you are creating a UIView as myView but adding view to self.view and even constraint also to view itself. So correct your code and replace view with myView. Secondly setTranslayesAutoresizingMaskIntoConstraints to false. Then add all the constraints to self.view. This should solve your problem.

     myView.setTranslatesAutoresizingMaskIntoConstraints(false)
     self.view.addConstraints([trailingConstraint, bottomConstraint, widthConstraint])
    

    VFL is also a better and clean approach. It actually gives a visualization of how constraint are setup.

提交回复
热议问题