I made a subclass of UIButton, and I want to set a “padding”. I want the button’s frame to always be 15 pixels wider on each side, than the text.
In the drawRect method, I set the frame of the button to be 30 px wider than the text’s width. But when I try to do this the frame does not get adjusted at all. Is this frame getting adjusted after I change the frame of my uibutton? How can I adjust the frame to set a padding?
Here is my code:
- (void)drawRect:(CGRect)rect{
[self setBackgroundImage:[[UIImage imageNamed:@"submitBtn.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal];
[self setFrame:CGRectMake(rect.origin.x, rect.origin.y-90, self.titleLabel.frame.size.width + 30, self.frame.size.height)];
}
Thanks for the answers!!
You should override the
setTitle:forState:method and adjust the frame there. You should also set the background image somewhere other thandrawRect:. A good place would be theinitXXXmethod.The only thing you should do in
drawRect:is actually render content for the view based on its current state. No state should be changed indrawRect:.