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

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

So, I move the buttonView by calling the setFrame method. This causes the buttons to shift and display correctly, but after that they do not respond to touch events. Buttons work correctly in representations of the type of the Parent class. In the view of the type of the child class as well, if I comment on the call to setFrame (), the buttons work correctly.

How can I change buttons and still respond to touch events?

Any help is appreciated.

The following are snippets of 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 .. } 
+4
source share
4 answers

A button may overlap with another view after changing the frame ... Just try setting the background color to transparent views so that you can get a clear idea that the view overlaps on the button.

+9
source

Make sure your UIButtons container can be interacted with. This is the one that bothers me. If your UIButton is a subtitle of another view (and not the main view of the view manager), it cannot be configured to allow user interaction (by default). Try: containerView.userInteractionEnabled = YES;

+7
source

Thanks for the help guys, you were right. I forgot to resize the scroll that I added above the buttons in the parent view. Thus, it overlapped with the shifted buttonView. Changing the background color helped me see this. Thanks for the tip, Chandan.

+3
source

This should work, I think, and it’s not in my head at all, it is that you have something that captures your touch events. Basically, there is another view on top of your button.

Good luck.

+2
source

Source: https://habr.com/ru/post/1345658/


All Articles