Programmatically creating constraints bound to view controller margins

前端 未结 3 1245
广开言路
广开言路 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.

    0 讨论(0)
  • 2020-12-08 21:47

    In your example code above, it seems like you are mixing up view and myView in a few places. In any event, widthConstraint should be added to myView and topConstraint, trailingConstraint, and bottomConstraint should be added to self.view. The reason for this is constraints must be added to the closest superview ancestor that lays out both views involved in the constraint. In the case where you are constraining a child view attribute to an attribute on its parent view, the constraint must be added to the parent view, since it lays out both itself and the child view. If you have a constraint between two sibling views, the constraint would be added to their parent view, since it is the closest ancestor that lays out both the views involved.

    If you're able to target iOS 9.0 and above, it's much cleaner and easier to use the new NSLayoutAnchor and NSLayoutDimension API for creating these kinds of constraints. It also provides strict type checking and the compiler can verify correctness. With these new APIs, your example code would simply become:

    let myView = UIView()
    myView.backgroundColor = UIColor.redColor()
    self.view.addSubview(myView)
    
    let margins = self.view.layoutMarginsGuide
    myView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true
    myView.topAnchor.constraintEqualToAnchor(margins.topAnchor).active = true
    myView.bottomAnchor.constraintEqualToAnchor(margins.bottomAnchor).active = true
    myView.widthAnchor.constraintEqualToConstant(300.0).active = true
    

    No need to explicitly add the constraints to the right view, etc. You can read more about this method of creating constraints here:

    https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutAnchor_ClassReference/

    and here:

    https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutDimension_ClassReference/

    0 讨论(0)
  • 2020-12-08 22:03

    Actually the problem in your code is that you did not set the translatesAutoresizingMaskIntoConstraints of myview to false, whenever you want to use auto-layout constraints then you have to set translatesAutoresizingMaskIntoConstraints of a view to false. Another Problem is that you do not add myview on self.view I have updated your code and Its working fine According your constraints.

    Put below code in your ViewController .

    let myView = UIView()
    myView.backgroundColor = UIColor.redColor()
    self.view.addSubview(myView)
    myView.translatesAutoresizingMaskIntoConstraints = false
    
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute:.Top, multiplier: 1, constant: 20))
    
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,multiplier: 1, constant: 300))
    view.addConstraint(NSLayoutConstraint(item: myView, attribute: .TrailingMargin, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1, constant: 0))
    
    0 讨论(0)
提交回复
热议问题