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.
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?
Post Details
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/swift/comme...