How to add a UIButton at runtime

I am trying to add a UIButton at runtime however it is not visible. What am I doing wrong?

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        btn.frame = CGRectMake(0, 0, 100, 25);
        btn.backgroundColor = [UIColor clearColor];
        [btn setTitle:@"Play" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buttonClick:)
        forControlEvents:UIControlEventTouchUpInside]; =;
        [self addSubview:btn];
    return self;


First, make sure the initWithFrame: method is being called. If your view is in a Nib, initWithCoder: is being called instead.

Second, is the button the only subview (from your code it looks like it is, but you never know). The button could be hidden behind another subview. Call bringSubviewToFront: if you need to.

Finally, is the view itself visible? Is it big enough to show the button? Given your example, if the view is less than 100 pixels wide, the button won't show because it will get clipped by the view's bounds.

Need Your Help

Ruby on Rails/Rack 1.4.5 return from Call Method / Can't Convert String to Array

ruby ruby-on-rails-3 rack

I am following Rob Conery in his Tekpub/Rails 3 tutorial video. I think something has changed in between the versions of Rack in video release (1.1) and the one on my machine (1.4.5). I don't know ...

Render element in repeter conditionaly

javascript html angularjs

Im tring to render items to page where I want to have five items per row. This means I want to have div element with row class only rendered every five items in array. Here is little pseudo code: