UIButton不可点击

时间:2013-07-01 09:09:31

标签: ios objective-c uibutton

我有一个功能,我将主视图移到侧面以显示UIButton。视图内部的按钮部分是可点击的,但按钮的右侧不是。 我已将self.view.frame更改为更大的数字,但我仍面临同样的问题。我也试过玩insertSubview:aboveSubview:,但这也没有帮助。

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
}

修改:我上传了一个问题示例项目https://dl.dropboxusercontent.com/u/137356839/TestMePlz.zip

3 个答案:

答案 0 :(得分:2)

在您向我发送代码后,我改变了一些内容:

首先,我创建了一个容器视图(可以更轻松地跟踪所有视图):

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

因此,您放入self.view的所有观看次数都转移到了_containerView。然后我显然将_containerView添加到self.view

而不是使用:

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

我做了:

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

您必须添加QuartzCore和CoreGraphics框架。

答案 1 :(得分:0)

请更正您的观点的位置:

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];

感谢。

答案 2 :(得分:0)

试试这个:

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

然后改变你的方法:

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

我在你的项目和按钮工作中做到了。