简体   繁体   中英

UIButton in custom UIView not responding to events

I have a custom UIView which I can use throughout the whole application in different places. 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. But the UIButton won't respond to the event, or to any event. It doesn't even highlight itself.
I did the initialisation before through a nib file with [[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0]; and connected the IBOutlets (which worked fine) and the IBAction s, which obviously, doesn't worked as well.

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. I've just tested it with the UIWindow color set to red.

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.

Edit2: No, the resizing of the UIViewController isn't working. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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