Pra*_*thi 4 iphone cocoa-touch uibutton ipad
我有一个视图控制器类(子)从视图控制器类(父)扩展.在父类的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 addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButtonView addSubview:myBackButton];
// Some code...
}
Run Code Online (Sandbox Code Playgroud)
在子类中:
- (void)loadView {
[super loadView];
//Some code ..
CGRect buttonViewRect = myButtonView.frame;
buttonViewRect.origin.y = yOffset; // This is basically original yOffset + 50
[myButtonView setFrame:buttonViewRect];
yOffset += KButtonViewHeight;
// Add some other view below myButtonView ..
}
Run Code Online (Sandbox Code Playgroud)
小智 7
确保您的UIButton的容器视图可以与之交互.这是让我受益的.如果您的UIButton是另一个视图的子视图(不是视图控制器的主视图),则可能未将其配置为允许用户交互(默认设置).尝试:containerView.userInteractionEnabled = YES;
| 归档时间: |
|
| 查看次数: |
7906 次 |
| 最近记录: |