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];
所以,如果你需要在运行时探测到superviews弄清楚什么是什么,你可以在下面使用此代码。 [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
** back的代码是什么:**。你在那里放置了一个'NSLog'来看看它是否触发了? – WrightsCS 2012-07-25 14:54:52
你可以尝试将按钮添加到'parentView'而不是'left'吗? – 2012-07-25 15:04:18
@EricBrotto它已经在我的问题。我试图做到这一点,只要按钮不落在图像视图的框架区域,它就会工作。 – Bourne 2012-07-25 15:08:10