UIButtons on tableFooterView not responding

I have successfully added some UIButtons to a custom tableFooterView. The problem is that button events are not triggered. In addition, an image regarding UIControlStateHighlighted does not appear. Any ideas? I tried everything I could think of, but I can't find a trick. I am reporting here an interesting part of the UITableViewController:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section 
{
    CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
    _footerView = [[UIImageView alloc] initWithFrame: footerFrame];
    _footerView.image = [UIImage imageNamed:@"tableviewfooter.png"];
    _footerView.contentMode = UIViewContentModeTopLeft;

    UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: @selector(addNewItem:) Image: @"additem.png" ImagePressed: @"additem_pressed.png"];
    [_footerView addSubview:addExam];

    return _footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
    return 190;
}


- (void) addNewItem:(id)sender
{
    // This get never called
    NSLog(@"Pressed");
}


- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    UIImage *newImage = [UIImage imageNamed: image];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return button;
}

Thank you for your help!

+3
source share
1 answer

I don’t think it UIImageViewwas designed to work well as a container for other species, especially UIButtons. First, UIImageViews, when created, has a userInteractionsEnableddefault property false.

UIView , UIButton, UIImageView.

+6

Source: https://habr.com/ru/post/1712512/


All Articles