iOS:How To Make AutoLayout Work On A SrollView

5992 ワード

転載元: http://natashatherobot.com/ios-autolayout-scrollview/
Posted on June 11 th、2014
Ok,I’ll admit.I’ve been seriously stugling with AutoLayout ever since it’s been introduced.I unders stand the concept,and I LOVE the idea of it,but when I actually do it,it almost never beveass.mys
So when I had a chance to go talk to an actual Apple Enginee about AutoLayout last week at WWWDC、I made sure to go.I thought of my most painful experience using AutoLayout recently–when I wans making a logn screen with username and password fields on a SrollView
ヘレシスwhat we made:
 
This is just two input fieldsセンターred on a SrollView.You can see the AutoLayout at work here–the twork input fields areセンターrectly both on a 4 s and a 5 s device.
This「simple」solution took the Apple Engeer 40 minutes to solive!However,several senior engineeers I know said that they’ve never been able to get AutoLayout working quite right on a ScrrollView、so 40 minutes is actually not bad!
Here are the key tricks to getting AutoLayout to work on a ScrrollView:
One View
The ScrrollView shound have only ONE child view.This forced in Android、so I shoud have made the connection、but I just didn’t think of it–s toease to put the two input filt elds right the Bures.Villectrew
Here is what the View Herarchy shoult actually look like:
Again、make sure to put all your fields、custom view inside the one child view of the ScrrollView!
Equal Widths
I’m going to start with the constrants from the outside(on the main view)in(to the stuff inside the ContentView)
The obvious starting point is to bind the SrollView to the View–just select the ScrrollView from the view hierarchy,and add the follwing constration:
The key to getting the constration to work properly however,is adding an Equal Width constrant between the main View.The ScrrollView adjust to the size of the content inside of it,so setting the Continew to the Width of the ScrelView leaves the dwitens
To create the Equal Width Costrant between the ContentView and the View、select the ContintView on the view hierarch and Control+Drag to the View–shout get a pop-up-up gives the
Your construaints between the main View、SrollView、and ContentView shound look like this:
Conttent Insets
The constress between the ScrrollView and the ContentView are surpringly stration forward–just bind the ContentView to the ScrrollView(make sure the constant to the bottom layoutugide 0):
The constrients between the ContentView and ScrrollView are now as follows with all constants set at 0:
If your storyboard is like mine,you might notice that the actual ContentView is not the full height of the main view or the SrollView:
However,we do want to make sure the ContentView is centred when it's rended on a device.To do that we need to write some code to property set the Contit the Cont Insets in the ViewController:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33// ViewController.swift  importUIKit  classViewController: UIViewController {                                  @IBOutlet var scrollView : UIScrollView    @IBOutlet var contentView : UIView                override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.    }      override func viewDidLayoutSubviews()    {        letscrollViewBounds = scrollView.bounds        letcontainerViewBounds = contentView.bounds                  varscrollViewInsets = UIEdgeInsetsZero        scrollViewInsets.top = scrollViewBounds.size.height/2.0;        scrollViewInsets.top -= contentView.bounds.size.height/2.0;                  scrollViewInsets.bottom = scrollViewBounds.size.height/2.0        scrollViewInsets.bottom -= contentView.bounds.size.height/2.0;        scrollViewInsets.bottom += 1                  scrollView.contentInset = scrollViewInsets    } 
  }Once you add the proper contraints into the ConttentView(see next step)、your final result will look like this:
The ugly colors are meant to differentiate the SrollView from the ContentView(red).Again,in,the storyboard,the ContentView the top of the ScrrollView,but with tens contentinest。
センターリングMultiple View
The final step is to add AutoLayout to the ContentView.This the same as adding layout normally to any view,so I won’t go into mucdetail here.
The one thing I did learn that I'd like to share(although now it seems ovious)is how to center the two text fields in the view.Previs、I put the two text fields into a containview
Instead,you can center each text field horizontally in container(so they’re now centred and on top of each other),and then add a constant of 25 to one(so it’s moved up 25 pixels from thentセンター),and stanta
 
This will leave you with a space of 50 pixels between the two text fields、but the spactly in between the m will be the center of the view.
Do YOU have any other AutoLayout tips?I’m always looking to learn more and improve、そしてplease let me know in the comments!
You can view the source code on Github here.