繁体   English   中英

添加多个子视图(> 4)时,UIButton停止响应触摸

[英]When adding several subviews ( > 4), UIButton stops responding to touches

我正在一个包含几个子视图的应用程序中创建一个模式视图。 这些子视图中的每个子视图都有一个按钮和与该按钮相关的动作。 我使用循环将每个子视图插入到位。 问题在于前4个子视图正常,从第5个子视图到最后一个子视图没有响应。

这是与问题相关的简化代码:

简化的SubviewView.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // More info button
        CGRect buttonFrame = CGRectMake(infoMarginLeft + infoWidthWithInfo, infoHeightControl, 25, 25);
        UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [button setFrame:buttonFrame];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    return self;
}

-(void)buttonTapped:(id)sender
{
    NSLog(@"Button tapped!";
}

简化的view.m

- (void)drawRect:(CGRect)rect
{ 
    for (int i = 0; i < 12; i++) {
        // Color info container
        SubviewView * miniView = [[SubviewView alloc] initWithFrame:CGRectMake(0, 20 * i, 15, 15)];
        miniColorView.backgroundColor = [UIColor clearColor];

        // Adding subview through array to keep track of objects
        [self addSubview:miniColorView];
    }
}

在此先感谢,我绝对不知道发生了什么事:p

-

编辑

刚发现那是什么!

父视图的高度与屏幕高度相同。 问题是,某些子视图超出了屏幕高度限制。 我要做的就是增加父视图的高度=)

我将检查以确保您没有在按钮上方添加视图miniView。

同样,这应该移到drawRect之外,只需将其放入您的init方法中即可。

编辑:

将miniView的背景颜色设置为除透明以外的其他颜色,然后查看是否仍然看到按钮。 如果不是,则掩盖了您的按钮,它将不会收到触摸事件。

暂无
暂无

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

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