簡體   English   中英

UIButton不可點擊

[英]UIButton is not clickable

我有一個函數,可以將主視圖移到側面以顯示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

將代碼發送給我后,我進行了一些更改:

首先,我創建了一個容器視圖(使跟蹤所有視圖更加容易):

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

所以,你把所有的意見self.view我搬到_containerView 然后,我明明添加了_containerViewself.view

而不是使用:

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

我做了:

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

您必須添加QuartzCore和CoreGraphics框架。

請更正您的視圖的位置:

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

謝謝。

嘗試這個:

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

然后更改您的方法:

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

我是在您的項目中完成的,並且按鈕有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM