简体   繁体   中英

UIButton is not clickable

I have a function where I move the main view to the side to expose a UIButton . The part of the button that's inside the view is clickable but the right side of the button is not. I've changed the self.view.frame to a much bigger number but I'm still facing the same problem. I've also tried playing around with insertSubview:aboveSubview: but that also didn't helped.

self.view.frame = CGRectMake(0, 0, 1600, 568);
_testView = [[UIView alloc]initWithFrame:CGRectMake(300, 20, 120, 100)];
_testView.backgroundColor = [UIColor greenColor];

UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame = CGRectMake(0, 0, 120, 100);
[test addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];

[_testView addSubview:test];
[self.view addSubview:_testView];

- (void)showRightMenu
{
    CGRect frame = self.view.frame;
    frame.origin.x = -100;
self.view.frame = frame
}

Edit: I've uploaded a sample project of my problem https://dl.dropboxusercontent.com/u/137356839/TestMePlz.zip

After you sent me your code I changed some things:

First I created a container view (makes it easier to track all the views):

_contatinerView = [[UIView alloc] initWithFrame:self.view.frame];

So all the views you put in self.view I moved to _containerView . Then I obviously added the _containerView to self.view .

Instead of using:

CGRect frame = self.view.frame;
frame.origin.x = -100;
self.view.frame = frame;

I did:

[_contatinerView setTransform:CGAffineTransformTranslate(_contatinerView.transform,-100, 0)];

You'll have to add the QuartzCore and CoreGraphics frameworks.

Please correct position of your View :

self.view.frame = CGRectMake(0, 0, 320, 568);
_testView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 100)];
_testView.backgroundColor = [UIColor greenColor];

UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame = CGRectMake(0, 0, 120, 100);
[test addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];

[_testView addSubview:test];
[self.view addSubview:_testView];

Thanks.

Try this:

[test addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

And then change your method:

-(void)doSomething:(id)sender
{
    NSLog(@"Bug Fixes:!!!!!!!");
}

I did it in your project and button works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM