添加到容器后未调用按钮

时间:2013-12-16 15:54:14

标签: ios uiview uibutton

我有容器视图,我在其中添加了一些图像和按钮。 容器视图是UIView的属性。

我会将我的容器视图添加到ScrollView,如下所示:

@property (nonatomic,strong) UIView *containerView;

...

containerView = [[UIView alloc] init];

    for (int i=0;i<5;i++) {

        UIImageView *whiteBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 235-height, 385-height-height)];
        whiteBack.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-131+height);
        whiteBack.backgroundColor = [UIColor darkGrayColor];
        whiteBack.layer.cornerRadius = 3;
        whiteBack.layer.masksToBounds = YES;

        [containerView addSubview:whiteBack];


        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 228-height, 340-height-height)];
        img.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-150+height);
        img.image = [UIImage imageNamed:[NSString stringWithFormat:@"poster0%i.jpg",i+1]];

       [containerView addSubview:img];

        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        infoButton.frame = CGRectMake(233, 0, 25, 25);

            infoButton.center =  CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2-100, 325-height);

        [infoButton addTarget:self action:@selector(flipImage:) forControlEvents:UIControlEventTouchUpInside];

        infoButton.tag = i;
        infoButton.backgroundColor = [UIColor clearColor];
        [infoButton setImage:[UIImage imageNamed:@"upload-50"] forState:UIControlStateNormal];
        [infoButton setTintColor:[UIColor whiteColor]];

        [containerView addSubview:infoButton];
    }

    [self.scrollView addSubview:containerView];

///

- (void) flipImage :(id) sender {

    NSLog(@"infoButton pressed:%i",[sender tag]);
}

但是当我按下按钮时没有任何事情发生!

1 个答案:

答案 0 :(得分:1)

您的容器视图的框架为零(因为您刚刚执行了[[UIView alloc] init];)。这意味着它永远不会接受任何触摸,因为你永远无法击中它。这也适用于子视图。

您看到子视图的原因是默认情况下,视图不会将绘图剪切到其边界。

要修复此问题,请将容器视图的框架设置为适合您要添加的子视图的大小。