小编Pra*_*thi的帖子

使用setFrame更改其位置后,UIButton不会响应触摸事件

我有一个视图控制器类(子)从视图控制器类(父)扩展.在父类的loadView()方法中,我创建了一个带有两个按钮的子视图(名为myButtonView)(按钮在子视图中水平布局)并将其添加到主视图中.在子类中,我需要将这两个按钮向上移动50像素.

所以,我通过调用setFrame方法来移动buttonView.这使得按钮可以正确移动和渲染,但在此之后它们不会响应触摸事件.按钮在Parent类类型的视图中正常工作.在子类类型视图中,如果我注释掉setFrame()调用按钮正常工作.

如何移动按钮并仍然让它们响应触摸事件?

任何帮助表示赞赏.

以下是代码的片段.

在父类中:

- (void)loadView {

// Some code...
    CGRect buttonFrameRect = CGRectMake(0,yOffset+1,screenRect.size.width,KButtonViewHeight);
    myButtonView = [[UIView alloc]initWithFrame:buttonFrameRect];
    myButtonView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:myButtonView];

// some code... 
    CGRect nxtButtonRect = CGRectMake(screenRect.size.width - 110, 5, 100, 40);
    myNxtButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myNxtButton setTitle:@"Submit" forState:UIControlStateNormal];
    myNxtButton.frame = nxtButtonRect;
    myNxtButton.backgroundColor = [UIColor clearColor];

    [myNxtButton addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [myButtonView addSubview:myNxtButton];

    CGRect backButtonRect = CGRectMake(10, 5, 100, 40);
    myBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myBackButton setTitle:@"Back" forState:UIControlStateNormal];

    myBackButton.frame = backButtonRect;
    myBackButton.backgroundColor = [UIColor clearColor];
    [myBackButton …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch uibutton ipad

4
推荐指数
2
解决办法
7906
查看次数

标签 统计

cocoa-touch ×1

ipad ×1

iphone ×1

uibutton ×1