开发者

Programmatically Created UIButtons Below Screen Not Working?

开发者 https://www.devze.com 2023-01-24 21:10 出处:网络
I am creating a matrix of boxes with some images in them. To manage the taps and get which box was tapped I am placing one UIButton inside of each box that is invisible and also the size of the box.

I am creating a matrix of boxes with some images in them. To manage the taps and get which box was tapped I am placing one UIButton inside of each box that is invisible and also the size of the box.

I added the event listener (sorry, I'm a Javascript programmer :) ) like this:

[butto开发者_Go百科n addTarget:self action:@selector(boxTapped:) forControlEvents:UIControlEventTouchUpInside];

I started the simulator up and it worked just fine. I can see the tag of the button in the log. However, I then scrolled down, and to my surprise, NONE of the buttons below the fold of the screen worked at all. No events were being sent. I am completely baffled by this and have no clue what would cause it. Does anyone have any ideas?

EDIT: Basically, as a summary, anything below the initial bottom of the screen never triggers an action. When half of a box is initially cut off, only the (top) half exposed when the view was first displayed works. The bottom half will not trigger the action.


One of the reasons is your buttons are actually placed outside your boxes' boundary. The buttons are still visible because you haven't set your boxes to clip anything outside the bound. And hence these buttons cannot receive any input event.

You can try to set clipsToBounds = YES for your boxes and see if the buttons are still visible?


Make sure the bounds for the view that handles boxTapped: is large enough to contain the buttons that are initially off-screen. A UIView can only respond to touches within its own coordinate system, however it can contain subviews that are outside its coordinate system that cannot be touched.

0

精彩评论

暂无评论...
验证码 换一张
取 消