Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton does not respond to touch events after changing its position using setFrame

I have a view controller class (child) which extends from view controller class (parent). In the parent class's loadView() method I create a sub-view (named myButtonView) with two buttons (buttons are horizontally laid out in the subview) and add it to the main view. In the subclass I need to shift these two buttons up by 50pixels.

So, I am shifting the buttonView by calling the setFrame method. This makes the buttons shift and render properly but they do not respond to touch events after this. Buttons work properly in the views of Parent class type. In the child class type view also, if I comment out the setFrame() call the buttons work properly.

How can I shift the buttons and still make them respond to touch events?

Any help is appreciated.

Following is snippets of the code.

In the parent class:

- (void)loadView {

// Some code...
    CGRect buttonFrameRect = CGRectMake(0,yOffset+1,screenRect.size.width,KButtonViewHeight);
    myButtonView = [[UIView alloc]initWithFrame:buttonFrameRect];
    myButtonView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:myButtonView];

// some code... 
    CGRect nxtButtonRect = CGRectMake(screenRect.size.width - 110, 5, 100, 40);
    myNxtButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myNxtButton setTitle:@"Submit" forState:UIControlStateNormal];
    myNxtButton.frame = nxtButtonRect;
    myNxtButton.backgroundColor = [UIColor clearColor];

    [myNxtButton addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [myButtonView addSubview:myNxtButton];

    CGRect backButtonRect = CGRectMake(10, 5, 100, 40);
    myBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myBackButton setTitle:@"Back" forState:UIControlStateNormal];

    myBackButton.frame = backButtonRect;
    myBackButton.backgroundColor = [UIColor clearColor];
    [myBackButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [myButtonView addSubview:myBackButton];
// Some code... 
}

In the child class:

- (void)loadView {

    [super loadView];


//Some code ..

    CGRect buttonViewRect = myButtonView.frame;
    buttonViewRect.origin.y = yOffset; // This is basically original yOffset + 50
    [myButtonView setFrame:buttonViewRect];

    yOffset += KButtonViewHeight;

// Add some other view below myButtonView ..    
}
like image 426
Pranathi Avatar asked Nov 29 '22 17:11

Pranathi


2 Answers

Button may have overlapped with another view after changing the frame... Just try by setting the background color to the views which are transparent so you can get clear idea which view is overlapping on button.

like image 151
Chandan Shetty SP Avatar answered Dec 07 '22 01:12

Chandan Shetty SP


Make sure your UIButton’s container view can be interacted with. This is the one that gets me. If your UIButton is a subview of another view (not your view controller’s main view), it may not be configured to allow user interaction (the default). Try: containerView.userInteractionEnabled = YES;

like image 34
nikhil Avatar answered Dec 07 '22 00:12

nikhil