2014-12-27 65 views
1

我在编程上在UIScrollView的视图中创建按钮。我也以编程方式创建UITextField。我可以选择并输入数据到文本字段整天。但我无法得到按钮被触摸的任何标志。我可以将它们添加到主视图中并且它们可以工作。但是,如果将其添加到UIScrollView内部的视图中,它们会在另一个维度中丢失。我已阅读大量有关设置userInteractiondelayContentTouches以及通过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

+1

请张贴一些代码样本,以你是如何将这些按钮将滚动型 – jervine10 2014-12-27 14:22:42

+0

您可以明确地启用'showsTouchWhenHighlighted',看看它甚至识别向下触摸事件。 – jervine10 2014-12-27 15:06:16

+0

我不认为它确实承认它。除非添加到“UIScrollView”之外的'UIView',否则您甚至不会获得文本闪烁。另外它不会导致委托人触发。 “UIScrollView”干扰或没有正确连接,但我迄今为止阅读的解决方案似乎不能防止这种干扰。似乎是一个常见问题。但文本字段毫不费力地工作。 – Bill3 2014-12-27 15:17:20

回答

2
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 
+1

这有助于找到答案。你的按钮工作。我没有。他们之间没有真正的区别。除位置。尽管看到按钮,尽管他们滚动,我没有我的'UIView'框架(partSetupView框架大小)设置为我的'UIScrollView'相同。我已经看到了这一点,但我不明白他们的意思,直到我自己与之战斗。 – Bill3 2014-12-27 18:15:20

+0

给你一个提升,因为你的评论把我放在正确的道路上! – PruitIgoe 2017-03-16 14:07:56