ios - 添加到容器后不调用按钮

标签 ios uiview uibutton

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

我将像这样将容器 View 添加到 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]);
}

但是当我按下按钮时没有任何反应!

最佳答案

您的容器 View 具有零帧(因为您刚刚执行了[[UIView alloc] init];)。这意味着它永远不会受到任何触摸,因为你永远无法击中它。这也适用于 subview 。

您看到 subview 的原因是,默认情况下, View 不会将绘图裁剪到其边界。

要解决此问题,请将容器 View 的框架设置为适合您要添加的 subview 的大小。

关于ios - 添加到容器后不调用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20615065/

相关文章:

ios - Desire2Learn:我如何将文件附加到讨论中的新帖子?

ios - 无法隐藏 ViewController 的状态栏嵌入在 NavigationController 中

ios - UIView动画最后被取消

ios - 在动画中更新 View 的框架

ios - UIButton 从不触发 Action 中的代码

iphone - 导航栏中的信息灯按钮

iOS9 Untrusted Enterprise Developer 没有信任选项

ios - Xcode 分配工具生成

ios - 在 iOS 运行时更改 subview

ios - 如果 UIButton 的大小是一定大小,如何更改它的大小