我有一个视图控制器类(子)从视图控制器类(父)扩展.在父类的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,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... }
在子类中:
- (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 .. }
解决方法
更改框架后,按钮可能与另一个视图重叠…只需尝试将背景颜色设置为透明视图,这样您就可以清楚地了解哪个视图在按钮上重叠.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。