Monday, August 19, 2013

iOS6 Problems: autoLayout and UI customization

When developing on iOS6 using Xcode, you might notice some changes. A few of these changes may have been documented while some were not. Still, one should read the documentations that Apple will release every update on iOS - especially developers.

One of the changes that Apple implemented is the new autoLayout feature. This feature makes creating interfaces much easier. It supports different screen sizes in your app and even makes internationalization almost trivial (no more new nibs or storyboards for languages you wish to support).

Even with this feature, there are times when you have to override the way the interface looks. Most of the time, we put the code in the viewDidLoad method then run the app and see the changes we made to the UI only to discover that it doesn't take effect at all or much worse, the UI is now messed up.

Why is this happening?

The reason is that you enabled autoLayout on the nib and put the code in the wrong method.

But isn't that where we usually put the code to alter some of the UI elements?

Yes, but if you are using autoLayout then you need to put it in a different method.

Where do we put the code then?

If you are using autoLayout and have some code to alter the UI, instead of putting it in the viewDidLoad method, you should put it in the viewDidLayoutSubviews.

As it turns out, the UI is rendered after the viewDidLoad is called so the changes to the UI are now gone and the app is rendered to how it should look by the autoLayout. The viewDidLayoutSubviews was created on iOS6 to accommodate any changes that will occur after the rendering. These changes may be in the UI or within the code itself.

So the next time you are developing in iOS6 and using the autoLayout feature, make sure to design your UI properly so you don't have to put some extra code to alter it. If you need to add some additional code after the layout is done, put in the viewDidLayoutSubviews instead of other methods. Also, read the documentation as it really helps a lot.

No comments:

Post a Comment