UIButton永远不会在UIImageView的框架区域内响应

时间:2012-07-25 14:26:45

标签: iphone ios ipad uiimageview uibutton

这是我的视图层次结构:parentView(UIView)有一个UIImageView作为其子视图,而子视图又有一个UIButton作为其子视图。

left = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left.png"]];
left.userInteractionEnabled = YES;
[parentView addSubview:left];

back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setImage:[UIImage imageNamed:@"Arrow-left.png"] forState:UIControlStateNormal];
[back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
back.showsTouchWhenHighlighted = YES;
[left addSubview:back];

一切都正常显示,但按钮不响应触摸。如果我将其框架从UIImageView的框架移动到其他地方并将其设置为父视图(UIView)的子视图,它会做出响应。但这就是事情。

即使我设置为parentView的subView,如果它位于UIImageView的框架区域内,该按钮也不会响应。对于图像视图,userInteractionEnabled属性已设置为YES。知道发生了什么事吗?

5 个答案:

答案 0 :(得分:1)

UIImageView关闭userInteraction - 打开它,按钮就可以工作。

编辑:

所以我几乎完全按照你所写的方式使用你的代码 - 一个红色的鲱鱼就是你说它看起来都很好。对我来说,自定义按钮的框架为0,0,0,0所以我什么都没看到。当我设置框架时,它完美地工作:

    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"46-truck.png"];
    assert(image);

    [back setImage:image forState:UIControlStateNormal];
    [back addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    back.showsTouchWhenHighlighted = YES;
    back.frame = (CGRect){ {0,0}, image.size};

NSLog(@"FRAME: %@", NSStringFromCGRect(back.frame) );
    [imageView addSubview:back];

因此,如果您需要在运行时探测超级视图以确定是什么,可以使用下面的代码。 [UIView dumpSuperviews:back msg:@“Darn Bark Button”];

@interface UIView (Utilities_Private)

+ (void)appendView:(UIView *)v toStr:(NSMutableString *)str;

@end

@implementation UIView (Utilities_Private)

+ (void)appendView:(UIView *)a toStr:(NSMutableString *)str
{
    [str appendFormat:@"  %@: frame=%@ bounds=%@ layerFrame=%@ tag=%d userInteraction=%d alpha=%f hidden=%d\n", 
        NSStringFromClass([a class]),
        NSStringFromCGRect(a.frame),
        NSStringFromCGRect(a.bounds),
        NSStringFromCGRect(a.layer.frame),
        a.tag, 
        a.userInteractionEnabled,
        a.alpha,
        a.isHidden
        ];
}

@end

@implementation UIView (Utilities)

+ (void)dumpSuperviews:(UIView *)v msg:(NSString *)msg
{
    NSMutableString *str = [NSMutableString stringWithCapacity:256];

    while(v) {
        [self appendView:v toStr:str];
        v = v.superview;
    }
    [str appendString:@"\n"];

    NSLog(@"%@:\n%@", msg, str);
}

+ (void)dumpSubviews:(UIView *)v msg:(NSString *)msg
{
    NSMutableString *str = [NSMutableString stringWithCapacity:256];

    if(v) [self appendView:v toStr:str];
    for(UIView *a in v.subviews) {
        [self appendView:a toStr:str];
    }
    [str appendString:@"\n"];

    NSLog(@"%@:\n%@", msg, str);
}

@end

答案 1 :(得分:0)

也许

parentView.userInteractionEnabled = YES;

或者父视图框架很小,不能按住按钮或图像视图。检查父视图的大小;

答案 2 :(得分:0)

我之前已经发生过这种情况......我认为这是吞噬事件的超级视图。

我知道您已声明将其添加到parentView只有在它不与图像视图重叠时才有效。如果在图像视图上禁用用户交互,则在添加到parentView时按钮事件是否会一致触发?

答案 3 :(得分:0)

您实际上需要设置userInteractionEnabled = NO以允许触摸通过。

取自here

答案 4 :(得分:-1)

left.userInteractionEnabled = YES;

将这一行放在最后。