Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton inside UIView not clickable

I'm a little stuck on this one, I have a UIButton inside a UIViewController, but for some reason the UIButton is not responding.

_downloadButton = [[DownloadButtonViewController alloc] init];
_downloadButton.issue = _issue;
_downloadButton.view.frame = CGRectMake(self.descriptionLabel.frame.origin.x, self.descriptionLabel.frame.origin.y + self.descriptionLabel.frame.size.height + 20, 200, 27);
[self.view addSubview:_downloadButton.view];

UIButton *tmpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tmpButton.titleLabel.text = @"test";
tmpButton.frame = CGRectMake(_downloadButton.view.frame.origin.x, _downloadButton.view.frame.origin.y - 30, _downloadButton.view.frame.size.width, _downloadButton.view.frame.size.height);
[self.view addSubview:tmpButton];

To see if nothing was overlapping it, I added another UIButton a little lower, even if I add it on the same position it still works.

The structure of my application is like this

- UIViewController
    - UIScrollView
        - ThumbCover
            - UIImageView
            - DownloadButton --> NOT WORKING
            - UIButton --> WORKING
            - UILabel
        - ThumbCover
            - UIImageView
            - DownloadButton --> NOT WORKING
            - UIButton --> WORKING
            - UILabel
        - ThumbCover
            - UIImageView
            - DownloadButton --> NOT WORKING
            - UIButton --> WORKING
            - UILabel
        - ThumbCover
            - UIImageView
            - DownloadButton --> NOT WORKING
            - UIButton --> WORKING
            - UILabel

The DownloadButton is just a simple UIButton, nothing special at all.

like image 877
woutr_be Avatar asked Sep 18 '12 03:09

woutr_be


People also ask

Why button is not clickable in Swift?

If your subview has a frame of size zero, you won't be able to tap the button even if you can see it. Using a view debugger from Xcode will show you if this is the issue. Also, check if the subview has user interaction enabled when adding it.

How can I check if UIButton is pressed?

If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again.


1 Answers

You have specified the height of the viewcontroller's view as 27

_downloadButton.view.frame = CGRectMake(self.descriptionLabel.frame.origin.x, self.descriptionLabel.frame.origin.y + self.descriptionLabel.frame.size.height + 20, 200, 27);

If your button's frame is outside the frame of its parent view (viewcontroller's view), the abovesaid frame, it wouldn't respond to your touches

like image 114
Nithin Avatar answered Oct 27 '22 19:10

Nithin