繁体   English   中英

无法触摸UIScrollView中的UIButton

[英]Can't touch UIButton in UIScrollView

我正在以编程方式在UIScrollView内的视图中创建按钮。 我还以编程方式创建了UITextField 我可以全天选择并将数据输入文本字段。 但是我没有任何迹象表明正在触摸按钮。 我可以将它们添加到主视图中并且它们可以工作。 但是,当将它们添加到UIScrollView内部的视图中时,它们会在另一个维度中丢失。 我已经阅读了很多有关设置userInteractiondelayContentTouches以及通过gestureRecognizer拦截的gestureRecognizer 最后一个可能与我的问题有关。 我通过[touch.view isDescendantOfView:self.myScrollViewName]返回NO 我无法通过[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];

partSetupViewUIScrollViewUIView

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM