Skip to main content

Adding UIView subclass programmatically not drawing itself



I don't understand how this works. If I draw a UIView object to my UIViewController .xib file, then my UIView redraws itself. If I add it to the subView like





CustomView : UIView





in UIViewController's viewDidLoad







CustomView *v = [[CustomView alloc] initWithFrame:self.view.frame];

[self.view addSubview:v];







The CustomView not draw itself. I then tried to do







[self.view setNeedsDisplay];







and I still get nothing. Just a white background (different than the black background I was getting before), but none of my drawing. How does it work when you add a UIView programmatically? thanks.


Comments

  1. You might simply be setting the frame of CustomView incorrectly.

    Each view has its own coordinate space, with (0, 0) at the upper left corner. A view's frame is in its parent's coordinate space. This mean that it is often wrong to set a view's frame to be the same as its superview's frame.

    A view's bounds is in the view's own coordinate space. So if you want to make your CustomView exactly cover its superview, you should set the CustomView's frame to its superview's bounds.

    Try this:

    CustomView *v = [[CustomView alloc] initWithFrame:self.view.bounds];

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?