Ios 自定义UIView中的UIButton未响应事件
我有一个自定义的Ios 自定义UIView中的UIButton未响应事件,ios,cocoa-touch,ios7,Ios,Cocoa Touch,Ios7,我有一个自定义的ui视图,我可以在整个应用程序的不同位置使用它。 基本上是这样的: @interface BottomBar : UIView { UIImageView *imageView; UILabel *nameLabel; UIButton *playPauseButton; } @end @implementation BottomBar - (id)initWithFrame:(CGRect)frame { self = [super init
ui视图
,我可以在整个应用程序的不同位置使用它。
基本上是这样的:
@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
我剪掉了一些方法,按下了按钮:(id)sender
方法也包括在内,别担心。但是,ui按钮
不会响应事件或任何事件。它甚至没有突出显示自己我之前是通过一个nib文件进行初始化的,
[[NSBundle mainBundle]loadNibNamed:@“BottomBar”[…]]objectAtIndex:0]代码>并连接了IBOutlets
(工作正常)和IBAction
s,显然,这两个接口也不工作
有什么办法解决吗
编辑:我认为它与这一部分更相关,因为我的UIViewController
似乎是重叠的,尽管它被定义为自由形式且绝对不重叠。我刚刚用ui窗口
颜色设置为红色对其进行了测试
下面是代码:
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];
OVERVIEWCONTROLLER在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)];
问题是您没有在代码中定义所需的ui按钮类型。我的ui视图控制器
与ui视图
重叠,因此无法识别点击。查看代码段。您可以看到父视图为44px高。每个子级最高40px高,并设置为y位置2。此外,这只是三种观点。而且它们都不重叠。很抱歉,你可以通过查看代码自己回答这个问题。我在回答之前已经检查了代码,它看起来是正确的。但是,我不知道您在代码的其余部分做了什么。这没有帮助,甚至注释掉所有内容,只是有一个空的默认按钮就有帮助了。playPauseButton=[[UIButton alloc]initWithFrame:cRectMake(2.0265.0,40.0,40.0)];试试这个代码你是说你的UIViewController视图重叠了底部栏吗?没错。我用一个包含其他ViewController的UIViewController
解决了这个问题。