简体   繁体   English

自定义UIView中的UIButton不响应事件

[英]UIButton in custom UIView not responding to events

I have a custom UIView which I can use throughout the whole application in different places. 我有一个自定义UIView ,可以在整个应用程序中的不同位置使用它。 It basically looks like this: 基本上看起来像这样:

@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

I snipped some methods, the buttonPressed:(id)sender method is included, don't worry. 我截断了一些方法,其中包括buttonPressed:(id)sender方法,请放心。 But the UIButton won't respond to the event, or to any event. 但是UIButton不会响应该事件或任何事件。 It doesn't even highlight itself. 它甚至没有突出显示自己。
I did the initialisation before through a nib file with [[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0]; 我之前通过带有[[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0];的nib文件进行了初始化[[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0]; and connected the IBOutlets (which worked fine) and the IBAction s, which obviously, doesn't worked as well. 并连接了IBOutlets (效果很好)和IBAction ,后者显然效果不佳。

Any ideas how to solve it? 有什么想法如何解决吗?

Edit: I think it's more related to this part, because it seems like my UIViewController is overlapping, although it's defined as a freeform and definitely not overlapping. 编辑:我认为它与这一部分更相关,因为我的UIViewController似乎是重叠的,尽管它被定义为自由格式,并且绝对不重叠。 I've just tested it with the UIWindow color set to red. 我刚刚将UIWindow颜色设置为红色进行了测试。

Here is the code anyway: 无论如何,这是代码:

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 is defined in a xib as 320x460, so it should have 44px on the bottom for my bar. OverviewViewController在xib中定义为320x460,因此它的底部应该有44px。

Edit2: No, the resizing of the UIViewController isn't working. Edit2:否,无法调整UIViewController的大小。 Any ideas how? 有什么想法吗?

Make sure that none of the views/labels are hiding your button (even a transparent view would not allow you to click). 确保所有视图/标签都没有隐藏您的按钮(即使是透明视图也不允许您单击)。 Also make sure that the bounds of that button is equal to its frame. 还要确保该按钮的边界等于其框架。

请注意,大多数时候imageView不允许您单击按钮,注释imageView代码,然后尝试。

Your code is 您的代码是

playPauseButton = [[UIButton alloc] initWithFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];

It should be 它应该是

playPauseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playPauseButton setFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];

The problem is you are not defining the UIButton Type in your code that is needed. 问题是您没有在所需的代码中定义UIButton Type

我的UIViewControllerUIView重叠,因此无法识别点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM