priyanka gautam
priyanka gautam

Reputation: 377

Unable to Add Button

I am unable to add button on UIView. I have UIView In which I want to add UIButton.
Here is my code which I am trying to implement.

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setMultipleTouchEnabled:NO];
            path = [UIBezierPath bezierPath];
            [path setLineWidth:1.0];


            _button = [UIButton buttonWithType:UIButtonTypeCustom];
            [_button setTitle:@"" forState:UIControlStateNormal];
            _button.userInteractionEnabled=YES;
            _button.frame =CGRectMake(50, 130, 100, 100);
            _button.backgroundColor =[ UIColor redColor];
            [_button addTarget:self
                       action:@selector(buttonClicked)
             forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:_button];

   }
    return self;
}

Upvotes: 0

Views: 63

Answers (2)

Abhinav
Abhinav

Reputation: 38162

Your button is being added to the view. I just tried with your code line by line and I could see it. I think problem is in your UIView frame. Probably you are not seeing it because it is crossing the boundary of your super UIView.

enter image description here

Upvotes: 1

Daniel T.
Daniel T.

Reputation: 33967

Instead of assigning to __button, make a local UIButton object and work with it. After the local button has been added to the view, assign it to _button.

Upvotes: 0

Related Questions