无法在UIScrollView中触摸UIButton

时间:2014-12-27 14:06:56

标签: ios uiscrollview uibutton

我在UIScrollView内的视图中以编程方式创建按钮。我还以编程方式创建了UITextField。我可以整天选择并在文本字段中输入数据。但我无法得到按钮被触摸的任何迹象。我可以将它们添加到主视图中并且它们可以工作。但是当添加到UIScrollView内的视图时,它们会在另一个维度中丢失。我已经阅读了大量关于设置userInteractiondelayContentTouches以及通过gestureRecognizer拦截的信息。最后一个可能与我的问题有关。我通过NO返回[touch.view isDescendantOfView:self.myScrollViewName]。我无法通过[touch.view isKindOfClass:[UIButton class]]触发它。这让我觉得我错过了一个更基本的错误?

相关: 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];

partSetupViewUIView中的UIScrollView

1 个答案:

答案 0 :(得分: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