【问题标题】:UIButtons receiving touch events when added directly to scrollview but not indirectlyUIButtons 在直接添加到滚动视图而不是间接添加时接收触摸事件
【发布时间】:2013-08-09 04:00:28
【问题描述】:

我的应用中有 10 个 UIButton 并排堆叠,按钮的总尺寸为 640x96。

如果我将这些按钮直接添加到 UIScrollView,它们会注册触摸事件并且可以滚动。

但如果我尝试将它们添加到普通 UIView,然后将该 UIView 添加到滚动视图,它们就不起作用。

这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
UIView *fview = [[UIView alloc] init];
fview.frame = CGRectMake(0, 0, 640, 96);
fview.backgroundColor = [UIColor blackColor];
[fview setUserInteractionEnabled:YES];
[_sv setUserInteractionEnabled:YES];

for (int i=0; i<10; i++)
{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(64*i, 0, 64, 96);
        [button setTitle:@"Click Me!" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [fview addSubview:button];
}
[_sv addSubview:fview];
_sv.contentSize = CGSizeMake(640, 96);
}

-(void)buttonClicked:(id)sender {
    NSLog(@"Clicked!");
}

【问题讨论】:

    标签: ios uiview uiscrollview uibutton subview


    【解决方案1】:

    在我写问题时,我解决了问题:D

    我所要做的就是在设置滚动视图的内容大小之前设置 UIView 的框架

    就我而言:

    [_sv addSubview:fview];
    fview.frame = CGRectMake(0, 0, 640, 96); // Added line
    _sv.contentSize = CGSizeMake(640, 96);
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 1970-01-01
      • 2018-10-30
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多