在UIScrollView中无法触摸UIButton
问题描述:
我在编程上在UIScrollView
的视图中创建按钮。我也以编程方式创建UITextField
。我可以选择并输入数据到文本字段整天。但我无法得到按钮被触摸的任何标志。我可以将它们添加到主视图中并且它们可以工作。但是,如果将其添加到UIScrollView
内部的视图中,它们会在另一个维度中丢失。我已阅读大量有关设置userInteraction
,delayContentTouches
以及通过gestureRecognizer
拦截的信息。最后可能与我的问题有关。我通过[touch.view isDescendantOfView:self.myScrollViewName]
返回NO
。我无法通过[touch.view isKindOfClass:[UIButton class]]
触发它。这让我觉得我有一个更重要的错误,我错过了?在UIScrollView中无法触摸UIButton
相关: https://stackoverflow.com/a/6825839/4050489
编辑以每个请求添加按钮的代码:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView
处于UIScrollView
一个UIView
。
答
Try this sample Code:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(@"Button Click");
}
@end
请张贴一些代码样本,以你是如何将这些按钮将滚动型 – jervine10 2014-12-27 14:22:42
您可以明确地启用'showsTouchWhenHighlighted',看看它甚至识别向下触摸事件。 – jervine10 2014-12-27 15:06:16
我不认为它确实承认它。除非添加到“UIScrollView”之外的'UIView',否则您甚至不会获得文本闪烁。另外它不会导致委托人触发。 “UIScrollView”干扰或没有正确连接,但我迄今为止阅读的解决方案似乎不能防止这种干扰。似乎是一个常见问题。但文本字段毫不费力地工作。 – Bill3 2014-12-27 15:17:20