Carl Thomas
Carl Thomas

Reputation: 3823

Custom UIView using Constraints Programmatically

If I create a standard UIView let aView = UIView() and add constraints to it programmatically the view will show on the screen.

However, if I create a custom view that inherits from UIView class CustomView: UIView then instantiate this view let aView = CustomView() and add constraints to it, the view does not appear on the screen. But if I add instantiate the custom view with a frame let aView = CustomView(frame: CGRectMake(0,0,100,100)) the custom view will appear.

Do you have to supply a frame to a custom view for it to show on the screen?

Updated:

My constraints are

let leftConstraint: NSLayoutConstraint = NSLayoutConstraint(item: aView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 50)

let topConstraint: NSLayoutConstraint = NSLayoutConstraint(item: aView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)

let rightConstraint: NSLayoutConstraint = NSLayoutConstraint(item: aView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0)

let bottomConstraint: NSLayoutConstraint = NSLayoutConstraint(item: aView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20)

I would like not to have to specify width and height constraints and just let the edges determine the size of the view.

Can this be done?

Upvotes: 0

Views: 1153

Answers (1)

MLyck
MLyck

Reputation: 5775

I'm making the assumption you are talking about programatic constraints:

Make sure your UIView has the following constraints:

Horizontal Positioning constraint Vertical Positioning Constraint

Horizontal Sizing Constraint (width) Vertical Sizing Constraint (Height)

Without a width and height constraint, your view is drawn. But it will have a 0 height and 0 width, thus you won't be able to see it.

Here's an example.

        // dictionary for views
    let viewsDictionary = [     "aView": aView]
    // dictionary for metrics
    let metricsDictionary = [   "ViewHeight":90]

// this places your UIVIew 16 points from the left, and makes it take up the rest of the space to the right. (until your self.view ends)
self.view.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "H:|-16-[aView]|", options: nil, metrics: nil, views: viewsDictionary))

// This constraint places it 32 points from the top. And gives it a height of 90
self.view.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "V:|-32-[aView(viewHeight)]", options: nil, metrics: metricsDictionary,
                views: viewsDictionary))

and make sure you disable autoLayout for your UIView.

Before you add it as subview add the following line:

aView.setTranslatesAutoresizingMaskIntoConstraints(false)

And no. You don't need to manually specify the frame. Just make sure the view has a width and a height sizing constraint.

Upvotes: 2

Related Questions