New filters on the Home Feed, take a look!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

4
Having trouble understanding Auto Layout and constraints
Post Body

Hey guys, I hope all is well. I'm currently attempting to learn more about Auto Layout and constraints programmatically, however I'm having a hard time understanding everything. Basically, I just want to create a small blue view as a subview in my main view, however as I increase the screen size (by selecting different device simulators), my blue rectangle gets bigger, and never stays the same size across device simulators. I thought with constraints and auto layout, the views would appear the same across devices?

Here's the below code that I'm working with:

mainView = UIView()

mainView.backgroundColor = UIColor.blue

view.addSubview(mainView)

mainView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        mainView.topAnchor.constraint(equalTo: view.topAnchor, constant: 350),
        mainView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -350),
        mainView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 150),
        mainView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -150)
        ])

I feel like I'm not grasping the concept of Auto Layout and constraints, and how to effectively use them.

Can you guys help point me in the right direction?

Author
Account Strength
80%
Account Age
11 years
Verified Email
Yes
Verified Flair
No
Total Karma
398
Link Karma
165
Comment Karma
233
Profile updated: 3 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago