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