[英]UIButton in UIView not responding to touch events despite userInteractionEnabled = true
[英]UIButton in custom UIView not responding to events
我有一个自定义UIView
,可以在整个应用程序中的不同位置使用它。 基本上看起来像这样:
@interface BottomBar : UIView
{
UIImageView *imageView;
UILabel *nameLabel;
UIButton *playPauseButton;
}
@end
@implementation BottomBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0.0, 524.0, 320.0, 44.0)];
if (self) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15.0, 2.0, 40.0, 40.0)];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 2.0, 180.0, 40.0)];
playPauseButton = [[UIButton alloc] initWithFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];
[self setBackgroundColor:[UIColor colorWithWhite:0.200 alpha:1.000]];
[imageView setImage:[UIImage imageNamed:@"NowPlaying.png"]];
[nameLabel setFont:[UIFont fontWithName:@"Helvetica Neue UltraLight" size:26.0]];
[nameLabel setShadowColor:[UIColor colorWithWhite:0.000 alpha:0.650]];
[nameLabel setShadowOffset:CGSizeMake(1.0, 2.0)];
[playPauseButton setUserInteractionEnabled:YES];
[playPauseButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
[playPauseButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:imageView];
[self addSubview:nameLabel];
[self addSubview:playPauseButton];
}
return self;
}
@end
我截断了一些方法,其中包括buttonPressed:(id)sender
方法,请放心。 但是UIButton
不会响应该事件或任何事件。 它甚至没有突出显示自己。
我之前通过带有[[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0];
的nib文件进行了初始化[[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0];
并连接了IBOutlets
(效果很好)和IBAction
,后者显然效果不佳。
有什么想法如何解决吗?
编辑:我认为它与这一部分更相关,因为我的UIViewController
似乎是重叠的,尽管它被定义为自由格式,并且绝对不重叠。 我刚刚将UIWindow
颜色设置为红色进行了测试。
无论如何,这是代码:
OverviewViewController *ovc = [[OverviewViewController alloc] init];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:ovc];
[[self window] setRootViewController:nvc];
bottomBar = [[BottomBar alloc] init];
[[self window] addSubview:bottomBar];
[[self window] bringSubviewToFront:bottomBar];
OverviewViewController在xib中定义为320x460,因此它的底部应该有44px。
Edit2:否,无法调整UIViewController
的大小。 有什么想法吗?
确保所有视图/标签都没有隐藏您的按钮(即使是透明视图也不允许您单击)。 还要确保该按钮的边界等于其框架。
请注意,大多数时候imageView不允许您单击按钮,注释imageView代码,然后尝试。
您的代码是
playPauseButton = [[UIButton alloc] initWithFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];
它应该是
playPauseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playPauseButton setFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];
问题是您没有在所需的代码中定义UIButton Type
。
我的UIViewController
与UIView
重叠,因此无法识别点击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.