I'm doing my first iOS app, and I'm a little bewildered by the way sizing and layout works (or, in my case, doesn't). I feel like what I'm trying to do is (or should be) very simple, but everything I try isn't having the desired result.
In my initial view controller, I have a UIScrollView
:
Programmatically, I'm adding a subview that's loaded from a XIB using .addSubView
:
var frame = scrollView.bounds
frame.origin.x = 0.0
frame.origin.y = 0.0
let view = pages[page] // pages[page] is UIView loaded from XIB
view.contentMode = .ScaleAspectFit
view.frame = frame
scrollView.addSubview(view)
So now on to the XIB (I changed the view to "Freeform" size from "Inferred" only for the purposes of taking this screenshot):
There are labels in three of the corners, and what I would like is for them to be aligned so that they always stay in those corners, and the centered text stays centered. When I run it, though, things are positioned in their same old boring places (I added a background so you can see the problem):
What I've tried (unsuccessfully):
- Using constraints to position the elements (doesn't seem to do anything, leading me to believe the problem is with the sizing of the child view
- Setting various size and layout properties of the child view
I am under a tight deadline, so my preference would be a way to do this that isn't too involved, but eventually I would like to have solid fundamentals and really know what's going on here. My question is multipart (but related):
- Given that I'm inexperienced, are constraints the best way to position the view elements so that they align left, right, center, etc.?
- What do I need to do to get the child views to size correctly in the
UIScrollView
? - Do I need to do anything special to re-layout if screen orientation changes?
Any advice would be helpful: specific techniques, tutorials, appropriate documentation (I've been reading the documentation, but it's a big forest, and I'm feeling a little lost).
This is a chance for one of you iOS wizards to shine with a great answer!