Ayear ago I've seen couple of Apple WWDC keynotes about benefits of Auto Layout, but when I tried them in real world projects, I experienced that when you move some UI element on a Storyboard even slightly, it breaks half of the constraints, nullifying all the benefits of Auto Layout.
Till now I've been utilising Springs & Struts quite successfully, and in conjunction with KVO observing frame path for view, handled most of rotation realignments gracefully, with just couple lines of code.
So now with iOS 7 coming out, I got a question: Can I keep successfully utilising Springs & Struts only for iOS 7 apps and avoid using Auto Layout? Or am I losing out on anything by doing so? Learning and mastering skill of using Auto Layout in different scenarios would probably take couple of days to me, so I'd really appreciate some argumented advice.
RESOLVED: Thanks to Max's answer, I started to dig into Auto Layout; while Null is right in his answer - you actually can use Springs & Struts on iOS 7 without too much trouble, Auto-Layout is powerful.
To delete a constraint you don't actually need to perform ancient shamanic dance of rain. You delete it and layout goes into warning mode, indicating issues you need to resolve to make Auto Layout work.
When frame in Interface Builder is actually different from calculated with layout constraints frame, Interface Builder will indicate the runtime frame of that element with red dotted line. Finally it is done right. Thanks, Apple.
Personally, I've been using Auto Layout since Xcode 4.6.3. The initial learning curve is painful, but after that it really becomes a joy to use. I'd definitely never go back to Springs & Struts or manually calculating metrics using frames.
Designed by US Open and PGA champion Larry Nelson the Cumberland River, our Scottish links-style, par-72 layout offers 18 challenging holes bordered by limestone bluffs. The course is enhanced by federally protected wetlands.
Gaylord Springs has earned its place among the nation's best. The course has won numerous awards throughout the years including being voted one of the Best Courses You Can Play by GolfWeek. At the signature fourth hole, you'll find the century-old springhouse from which the club derived its name.
The algorithm simulates a force-directed representation of the networktreating edges as springs holding nodes close, while treating nodesas repelling objects, sometimes called an anti-gravity force.Simulation continues until the positions are close to an equilibrium.
Set the random state for deterministic node layouts.If int, seed is the seed used by the random number generator,if numpy.random.RandomState instance, seed is the randomnumber generator,if None, the random number generator is the RandomState instance usedby numpy.random.
The SpringLayout class was added in JDK version 1.4 to support layout in GUI builders. SpringLayout is a very flexible layout manager that can emulate many of the features of other layout managers. SpringLayout is, however, very low-level and as such you really should only use it with a GUI builder, rather than attempting to code a spring layout manager by hand.
This section begins with a simple example showing all the things you need to remember to create your first spring layout and what happens when you forget them! Later it presents utility methods that let you lay out components in a couple of different types of grids.
Spring layouts do their job by defining directional relationships, or constraints, between the edges of components. For example, you might define that the left edge of one component is a fixed distance (5 pixels, say) from the right edge of another component.
In a SpringLayout, the position of each edge is dependent on the position of just one other edge. If a constraint is subsequently added to create a new binding for an edge, the previous binding is discarded and the edge remains dependent on a single edge.
Unlike many layout managers, SpringLayout does not automatically set the location of the components it manages. If you hand-code a GUI that uses SpringLayout, remember to initialize component locations by constraining the west/east and north/south locations. Depending on the constraints you use, you may also need to set the size of the container explicitly.
Components define edge properties, which are connected by Spring instances. Each spring has four properties its minimum, preferred, and maximum values, and its actual (current) value. The springs associated with each component are collected into a SpringLayout.Constraints object.
An instance of the Spring class holds three properties that characterize its behavior: the minimum, preferred, and maximum values. Each of these properties may be involved in defining its fourth, value, property based on a series of rules.
An instance of the Spring class can be visualized as a mechanical spring that provides a corrective force as the spring is compressed or stretched away from its preferred value. This force is modelled as linear function of the distance from the preferred value, but with two different constants -- one for the compressional force and one for the tensional one. Those constants are specified by the minimum and maximum values of the spring such that a spring at its minimum value produces an equal and opposite force to that which is created when it is at its maximum value. The difference between the preferred and minimum values, therefore, represents the ease with which the spring can be compressed. The difference between its maximum and preferred values indicates the ease with which the Spring can be extended.
This section takes you through the typical steps of specifying the constraints for a container that uses SpringLayout. The first example, SpringDemo1.java, is an extremely simple application that features a label and a text field in a content pane controlled by a spring layout. Here is the relevant code:
Obviously, we have some problems. Not only does the frame come up way too small, but even when it is resized the components are all located at (0,0). This happens because we have set no springs specifying the components' positions and the width of the container. One small consolation is that at least the components are at their preferred sizes we get that for free from the default springs created by SpringLayout for each component.
The first putConstraint call specifies that the label's left (west) edge should be 5 pixels from its container's left edge. This translates to an x coordinate of 5. The second putConstraint call sets up a similar relationship between the top (north) edges of the label and its container, resulting in a y coordinate of 5.
The first putConstraint call makes the text field's left (west) edge be 5 pixels away from the label's right (east) edge. The second putConstraint call is just like the second call in the first snippet, and has the same effect of setting the component's y coordinate to 5.
The first putConstraint call makes the container's right edge be 5 pixels to the right of the text field's right edge. The second one makes its bottom edge be 5 pixels beyond the bottom edge of the tallest component (which, for simplicity's sake, we've assumed is the text field).
In this case the spring layout has chosen to give all the extra space to the text field. Although it seems like the spring layout treats labels and text fields differently, spring layout has no special knowledge of any Swing or AWT components. It relies on the values of a component's minimum, preferred, and maximum size properties. The next section discusses how spring layout uses these properties, and why they can cause uneven space distribution.
A SpringLayout object automatically installs Springs for the height and width of each component that the SpringLayout controls. These springs are essentially covers for the component's getMinimumSize, getPreferredSize, and getMaximumSize methods. By "covers" we mean that not only are the springs initialized with the appropriate values from these methods, but also that the springs track those values. For example, the Spring object that represents the width of a component is a special kind of spring that simply delegates its implementation to the relevant size methods of the component. That way the spring stays in sync with the size methods as the characteristics of the component change.
When a component's getMaximumSize and getPreferredSize methods return the same value, SpringLayout interprets this as meaning that the component should not be stretched. JLabel and JButton are examples of components implemented this way. For this reason, the label in the SpringDemo3 example does not stretch.
The getMaximumSize method of some components, such as JTextField, returns the value Integer.MAX_VALUE for the width and height of its maximum size, indicating that the component can grow to any size. For this reason, when the SpringDemo3 window is enlarged, SpringLayout distributes all the extra space to the only springs that can grow those determining the size of the text field.
The SpringDemo examples used the SpringLayout method putConstraint to set the springs associated with each component. The putConstraint method is a convenience method that lets you modify a component's constraints without needing to use the full spring layout API. Here, again, is the code from SpringDemo3 that sets the location of the label:
As the preceding snippets imply, SpringLayout and SpringLayout.Constraints tend to use different conventions for describing springs. The SpringLayout API uses edges to define its constraints. Springs connect edges to establish linear relations between them. Edges are defined by components, using the following constants:
Because the SpringLayout class was created for GUI builders, setting up individual springs for a layout can be cumbersome to code by hand. This section presents a couple of methods you can use to install all the springs needed to lay out a group of components in a grid. These methods emulate some of the features of the GridLayout, GridBagLayout, and BoxLayout classes.
3a8082e126