0

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?

1
  • No, you don't need to supply a frame manually if you're using autolayout. Can you share the constraints that you're applying to aView when it's not appearing and someone might be able to help. Commented Mar 23, 2015 at 0:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way of setting the constraints without setting the view width and height? Just by setting the top, bottom, left and right constraints? (I have updated my question with my current constraints in)
yeah of course. For example, instead of setting a specified height, if you want it to start 32 points from the top and 64 from the bottom. You would just change it to "V:|-32-[aView]-64-|" Then it would take up the rest of the space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.