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

Wildcards in a hosts file

I want to setup my local development machine so that any requests for *.local are redirected to localhost . The idea is that as I develop multiple sites, I can just add vhosts to Apache called site1.local , site2.local etc, and have them all resolve to localhost , while Apache serves a different site accordingly.