Wednesday, April 24, 2013

Why is my view disappearing in landscape orientation when using Autolayout?

Have you come across this particular scenario where you enable Autolayout, drag a view onto the parent view, change the orientation of the parent view to landscape and the view just disappears? If you rotate it back to portrait, the view comes back. I used to find this really confusing. There is a very logical explanation as to why this happens.

Here is my parent view with the view I dragged on to it.


Simple enough! You probably already know that the Autolayout feature uses constraints to figure out where a particular object is located within a view. So here is what the constraints look like for my view.

The parent view has a size of 320 x 460 in portrait mode which changes to 480 x 300 in landscape mode. So the reason for the disappearing view is quite simple.

When we have constraints for all sides of the view, it means that the child view will be resized automatically to meet those constraints regardless of what orientation it is in. The width of the child view is the width of the parent view minus the sum of the horizontal space constraints. Similarly, the height of the child view is the height of the parent view minus the sum of the vertical space constraints. In the portrait mode of this particular example, the child view has a width of 320 - (85 + 75) = 160, and a height of 460 - (95 + 205) = 160. And to verify that, all you have to do is select the child view and open up the size inspector and sure enough there it is.

Now let's see what happens when the view is rotated to landscape. As per our previous calculations the width of the rotated view should be 480 - (85 + 75) = 320 and the height of the new view will be 300 - (95 + 205) = 0. That's why you don't see the view in landscape, its height is zero. If you select the view in the document outline, you can see that the view has no height.

This problem occurs because we have 4 constraints describing how far away the child view should be from the 4 sides of the parent view and since those constraints are not flexible, the child view has to be resized to meet those constraints. If you want to see your child view in the same size even in the landscape mode, here is what you do.

1. Change the orientation to portrait and you can see your view.
2. Fix the width and height of your view by selecting the child view and selecting the menu option Editor->Pin->Width and Editor->Pin->Height.
3. Now that the height and width of your child view is fixed, remove the constraints for the bottom and right of the child view.

This is what the list of constraints looks like now:

Now rotate the view to landscape and voila your view is still visible.

Thursday, April 18, 2013

How to track allocations and deallocations when using ARC

If you are new to ARC(Automatic Reference counting), you might feel that you have lost control of the allocations and deallocations of objects. Well, the truth is, that's why ARC was invented. So you don't need to track the allocations and deallocations. But sometimes for debugging purposes it might help to know when a particular object is being allocated and deallocated. A very simple method for this is to add the following lines of code to the class whose object you want to track. The example below is for a view controller class



You will observe that as you run your app, the init and dealloc log messages will be displayed in the xcode console as and when your object gets allocated and deallocated.

If you happened to get some error messages saying that an initWithCoder function could not be found, then just override the init function instead of initWithCoder. Classes derived from NSObject would not have an initWithCoder function unless it implements the NSCoding protocol.

You shouldn't have to track allocations and deallocations regularly if you are using ARC because ARC does a pretty good job of handling it. But in the rare occasion when you feel something is not going as planned, it doesn't hurt to double check.

Did you find this helpful? Do you have a special way of tracking memory? Do let me know.

Why don't I see the autosizing feature in the new xcode 4.2?

In the new XCode 4.2, when you start a new project with the default settings, you may not find the autosizing feature. This is what the size inspector will probably look like:


Nothing to worry. There is a very easy way to display it. Select your storyboard file and then on the file inspector. You will notice that the 'Use Autolayout' option is selected. Well, that is exactly why you don't see the autosizing view. Just uncheck the 'Use Autolayout' option. Now if you check the size inspector, voila, there it is.


Hope this helps.