0

I want to add view on my View, but they are not set well this width stretch. I am new to iOS. Any help would be appreciated. This is what I get when I try to do what I want, can someone help to fix this:

   openView.translatesAutoresizingMaskIntoConstraints = false
        let topConstraint = NSLayoutConstraint(item: openView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
        let bottomConstraint = NSLayoutConstraint(item: openView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
        let leadingConstraint = NSLayoutConstraint(item: openView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
        let trailingConstraint = NSLayoutConstraint(item: openView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0)

        self.view.addSubview(openView)
       openView.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
        openView.layoutIfNeeded()

2 Answers 2

2

In your xcode open main.Storyboard --> Open your viewController -->Add UIView --> Make your view frame like you want --> And add constraints

Ex: If you want width=600 & height=600, set frame like you want and then assign constraints.

.Refer linked image for referenceenter image description here

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

1 Comment

Hey i have just edit my answer refer it .It will solve your problem.
0

Here you are setting subview's frame as the initial frame of controller view. ie, the controller's view will be fully covered by subview.

If you want to add constraints,

1) Take the storyboard or xib.

2) Tap on the UI element.

3) Tap on the constraints button as shown in image.

4) Set the values from each border.

You can also set height, width such like parameters. The whole need is the system should be able to interpret the accurate position from the constraints.

3 Comments

I want to set full view.
Then set the constraints to 0 for each side.
It is no my xib it seperate view . there is no option for setting constarint

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.