RSS Feed!

Recent Posts

Recent Comments

Custom UIView didn't want to render controls

… from the amazing world of iPhone development …

Today I created a UIView subclass that belongs to my own UIViewController.
In my UIView drawRect method I create a few components:  a UILabel, a UITextField and a UIButton and then I add them all via [self addSubview: component]. 
The application built and ran successfully but I then discovered that with the exception of the UILabel the other components only had their shape "drawn" on the screen but the default labels where not actually displayed.
After a bit of unsuccessful googling and forum digging I decided to move all the code from drawRect to the constructor since nothing really made sense. To my surprise that fixed it. Fortunately I then decided that I cannot allow for all that code to be in the init method so I did some API digging and found three methods that looked promising. I then tried them all and one actually solved my problem.
So behold the scientifically discovered solution to all programmatically defined GUIs: the  UIView's layoutIfNeeded method! Append all your subviews and once you're done just call it.
Cheers…

Tags:

By Nick | 3. Mar 2009 | Objective C, iPhone | 1 Comment »

One Comment

  1. Anonymous says:

    Thanks for the post. After spending an hour on a similar problem, I hit google and came across your post.
    [self layoutIfNeeded];
    fixed it.
    Thanks

Leave a Reply