Search code examples
iosobjective-cuibuttondrawrectibaction

Changing UIButton frame disconnects it from IBAction selector


I have a xib file which contains a UIButton. I control + dragged the button to create an action. The position of the button has to be set dynamically. The method below is called from drawRect: method which updates the button position.

-(void)updateButtonPosition {
    CGFloat xPos = self.label.frame.origin.x + self.label.frame.size.width;
    CGFloat yPos = self.label.frame.origin.y + self.label.frame.size.height - 25;
    CGRect frame = CGRectMake(xPos, yPos, self.button.frame.size.width, self.button.frame.size.height);
    self.button.frame = frame;
}

But I noticed that calling this function disconnects the UIButton from the action I created by control + dragging (i.e. nothing happens when I click the button when the app is running). I commented the part where this function was called. And now the button started working and the action method was called. So changing the frame disconnected the UIButton from its action method.

EDIT: User Interaction is enabled for all its superview. I'm not using auto layout. The project is like that.

How can I change the frame of the button so that the action is also called when I click the button?


Solution

  • My button's frame was outside the frame of its parent view. Hence it was not tap able.

    For more info refer UIButton not responding after set frame within UIScrollview