Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Code formatting cleanup and whatnot.

  • Loading branch information...
justin committed Mar 2, 2016
1 parent ba6403e commit b0ac2a9390f3ed1e83ee015b82b1f72b2921de71
@@ -56,8 +56,8 @@ The purpose of the code is to center a UIView `orangeBoxView` in its superview w
Upon rotation, the box remains the same size but is no longer centered in its superview. That's not a bug you caused necessarily. You defined the frame based on the coordinates you were working with at the time you called `setFrame:`. To adjust the box to handle both orientations, however, you need to do a recalculation at the time the frameworks tell you that the device is transitioning to a different size class.

~~~swift
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator
coordinator: UIViewControllerTransitionCoordinator)
override func viewWillTransitionToSize(size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
let viewFrame = self.view.bounds
@@ -99,20 +99,19 @@ Not all views have an intrinsic content size, but things like buttons and labels
Let's look at an example of this tweet button view. Inside are both a `UIImageView` with the home icon and a `UILabel` that says "Home" in English. In our view subclass, we can override the `intrinsicContentSize` method to calculate and return a value as a `CGSize`.

~~~swift
override func intrinsicContentSize() -> CGSize
{
let attributes = [
NSFontAttributeName : UIFont.systemFontOfSize(12.0),
NSForegroundColorAttributeName : UIColor.blackColor()
]
override func intrinsicContentSize() -> CGSize {
let attributes = [
NSFontAttributeName : UIFont.systemFontOfSize(12.0),
NSForegroundColorAttributeName : UIColor.blackColor()
]
let labelSize = self.label.text.sizeWithAttributes(attributes)
let imageSize = self.image.size
let labelSize = self.label.text.sizeWithAttributes(attributes)
let imageSize = self.image.size
let width = imageSize.width + labelSize.width;
let height = max(imageSize.height, labelSize.height);
let width = imageSize.width + labelSize.width;
let height = max(imageSize.height, labelSize.height);
return CGSizeMake(width, height)
return CGSizeMake(width, height)
}
~~~

@@ -198,4 +197,4 @@ Grab your apron. We are about to learn how the sausage is made.

[css]: http://gridstylesheets.org/guides/ccss/

\pagebreak
\pagebreak
@@ -76,45 +76,31 @@ let isPhone = (device.userInterfaceIdiom == UIUserInterfaceIdiom.Phone)
let isFivePhone = (screenHeight == 568.0)
let isSixPhone = (screenHeight == 667.0)
let isSixPlusPhone = (screenHeight == 736.0)
if (UIDeviceOrientationIsPortrait(currentOrientation) == true)
{
// Do Portrait Things.
if (isPhone == true)
{
// Don't deny you've done this at least once.
if (isFivePhone == true)
{
// iPhone 5+
}
else if (isSixPhone == true)
{
// iPhone 6
}
else if (isSixPlusPhone == true)
{
// iPhone 6 Plus
}
else
{
// Old iPhones
}
if (UIDeviceOrientationIsPortrait(currentOrientation) == true) {
// Do Portrait Things.
if (isPhone == true) {
// Don't deny you've done this at least once.
if (isFivePhone == true) {
// iPhone 5+
}
else
{
// Do Portrait iPad Things.
else if (isSixPhone == true) {
// iPhone 6
}
}
else
{
// Do Landscape Things.
if (isPhone == YES)
{
// Do Landscape iPhone Things.
}
else
{
// Do Landscape iPad Things.
else if (isSixPlusPhone == true) {
// iPhone 6 Plus
} else {
// Old iPhones
}
} else {
// Do Portrait iPad Things.
}
} else {
// Do Landscape Things.
if (isPhone == YES) {
// Do Landscape iPhone Things.
} else {
// Do Landscape iPad Things.
}
}
~~~

@@ -168,30 +154,29 @@ Now, let's build and run our application in the iPhone simulator. You'll notice
In fact, if you add the following lines of code to your `ViewController` implementation, you can see exactly what is going on:

~~~swift
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
print("self.view.constraints = \(self.view.constraints)")
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("self.view.constraints = \(self.view.constraints)")
}
~~~

Now if you run the application, you'll see a giant array of auto-generated constraints printed into the debugging console.

self.view.constraints = [<_UILayoutSupportConstraint:0x7ff23b418150
V:[_UILayoutGuide:0x7ff23b4232c0(20)]>, <_UILayoutSupportConstraint:
0x7ff23b426170 V:|-(0)-[_UILayoutGuide:0x7ff23b4232c0]
(Names: '|':UIView:0x7ff23b418c80 )>,
<_UILayoutSupportConstraint:0x7ff23b41b7d0
V:[_UILayoutGuide:0x7ff23b423f70(0)]>,
<_UILayoutSupportConstraint:0x7ff23b417860
_UILayoutGuide:0x7ff23b423f70.bottom == UIView:0x7ff23b418c80.bottom>,
<NSIBPrototypingLayoutConstraint:0x7ff23b425000 'IB auto generated at build
time for view with fixed frame' H:|-(129)-[UILabel:0x7ff23b419040'Label']
(LTR) (Names: '|':UIView:0x7ff23b418c80 )>,
<NSIBPrototypingLayoutConstraint:0x7ff23b4254c0 'IB auto generated at build
time for view with fixed frame' V:|-(164)-[UILabel:0x7ff23b419040'Label']
(Names: '|':UIView:0x7ff23b418c80 )>
... ])
self.view.constraints = [<_UILayoutSupportConstraint:0x7ff23b418150
V:[_UILayoutGuide:0x7ff23b4232c0(20)]>, <_UILayoutSupportConstraint:
0x7ff23b426170 V:|-(0)-[_UILayoutGuide:0x7ff23b4232c0]
(Names: '|':UIView:0x7ff23b418c80 )>,
<_UILayoutSupportConstraint:0x7ff23b41b7d0
V:[_UILayoutGuide:0x7ff23b423f70(0)]>,
<_UILayoutSupportConstraint:0x7ff23b417860
_UILayoutGuide:0x7ff23b423f70.bottom == UIView:0x7ff23b418c80.bottom>,
<NSIBPrototypingLayoutConstraint:0x7ff23b425000 'IB auto generated at build
time for view with fixed frame' H:|-(129)-[UILabel:0x7ff23b419040'Label']
(LTR) (Names: '|':UIView:0x7ff23b418c80 )>,
<NSIBPrototypingLayoutConstraint:0x7ff23b4254c0 'IB auto generated at build
time for view with fixed frame' V:|-(164)-[UILabel:0x7ff23b419040'Label']
(Names: '|':UIView:0x7ff23b418c80 )>
... ])
Most of the constraints are using the private `NSIBPrototypingLayoutConstraint` class to define the constraints. If you don't explicitly define the constraints you want your view to take advantage of, Interface Builder will create a suite of `NSIBPrototypingLayoutConstraint` constraints it believes will accomplish what you are trying to lay out.
@@ -32,7 +32,7 @@ Fortunately, with iOS 8, Apple added the ability to enable and disable constrain

The first thing I tend to do is disable any existing constraints I know I won't need for a certain size class. You can quickly do this in the sidebar of Interface Builder under the constraints listing for our view controller. To start, select the "Image View.centerX = centerX" constraint. This is the constraint we created in the previous chapter to ensure that our DeveloperTown logo was always centered vertically on the screen. Since we are now moving the image view to the left of our text fields, that's no longer the case.

To disable it, make sure the Identity Inspector is visible. (You can toggle it from the View menu under Utilities > Show Identity Inspector.) You should see the values for our CenterX attribute on the right side of the Xcode window. At the bottom of that inspector, there is a small plus icon and a checkbox that says "Installed." Click the checkbox and then "Any Width | Compact Height (current)."
To disable it, make sure the Identity Inspector is visible. (You can toggle it from the View menu under Utilities > Show Identity Inspector.) You should see the values for our CenterX attribute on the right side of the Xcode window. At the bottom of that inspector, there is a small plus icon and a checkbox that says "Installed." Click the plus icon and then choose "Any Width | Compact Height (current)."

![Adjusting constraints per trait collection](./images/ch06-ss03.png)

@@ -104,7 +104,7 @@ In general, you should opt for activating and deactivating constraints rather th

### Summary

In this chapter, we moved our knowledge of size classes from theoretical to practical by adding a different set of constraints for our DeveloperTown screen in the landscape orientation. We also learned how to use the Identity Inspector in iPad to adjust different values on our constraints. Finally, we covered what happens when we activate and deactivate constraints in our views.
In this chapter, we moved our knowledge of size classes from theoretical to practical by adding a different set of constraints for our DeveloperTown screen in the landscape orientation. We also learned how to use the Identity Inspector in Xcode to adjust different values on our constraints. Finally, we covered what happens when we activate and deactivate constraints in our views.

Next, we are going to continue our deep dive into Auto Layout by writing some code to interact with our constraints.

0 comments on commit b0ac2a9

Please sign in to comment.
You can’t perform that action at this time.