UIButton visible but cannot be pressed

35 Views Asked by At

My buttons can’t be pressed. When I press them there is no animation or highlight, and the onPressed method never gets called either.

I strongly suspect there’s something wrong with the way I’m adding subviews.

My buttons are in a UIView called QuickMove. My objects are like this: BoardView contains EvilEndgame instance, EvilEndgame instance contains QuickMove instance.

EvilEndgame UIView init’s QuickMove and adds it to the subview:

EvilEndgame.m

-(void) initializeChessBoard: (UIView*) boardView boardDict: (NSMutableDictionary*) board {
    // Properties
    self.engine = [[NSMutableDictionary alloc] init];

    // ScoresLayer
    self.scoresLayer = [ScoresLayer sharedInstance];
    self.scoresLayer.boardView = boardView;
    self.scoresLayer.board = board;
    
//     

    // QuickMove
    self.quickMove = [QuickMove sharedInstance];
    [self.quickMove startNewGame];
    [boardView addSubview: self.quickMove];
    self.quickMove.delegate = self;
}

QuickMove creates multiple buttons and adds them via [self addSubview: button]:

QuickMove.m

+ (instancetype)sharedInstance {
    static QuickMove *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[QuickMove alloc] init];
        // Set the bounds to match the screen bounds
        sharedInstance.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        // Set the center to adjust the position
        sharedInstance.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, 415 + [UIScreen mainScreen].bounds.size.height / 2);
    });
    return sharedInstance;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor grayColor];
    }
    return self;
}

- (void)startNewGame {
    for (UIButton *button in self.buttons) {
        [button removeFromSuperview];
    }
    for (UIButton *button in self.smartMoveButtons) {
        [button removeFromSuperview];
    }
    [self.buttons removeAllObjects];
    [self.smartMoveButtons removeAllObjects];
    
    [self createButtons];
}

- (void)createButtons {
    self.buttons = [[NSMutableArray alloc] init];
    self.smartMoveButtons = [[NSMutableArray alloc] init];
    CGFloat buttonWidth = self.bounds.size.width / 8; // 8 original buttons
    CGFloat buttonHeight = 50;
    CGFloat smartMoveWidth = 50; // 3 smartMove buttons

    // Create smartMove buttons with spacing
    CGFloat spacing = 1;
    int tag = 100; // Start smart button tags from 100
    for (int i = 1; i <= 3; i++) {
        CGFloat offset = (i - 1) * (smartMoveWidth + spacing);
        UIButton *smartMoveButton = [self createButtonWithTitle:[NSString stringWithFormat:@"SM%d", i]
                                                          frame:CGRectMake(offset, 0, smartMoveWidth, buttonHeight)
                                                            tag:tag++];
        [self.smartMoveButtons addObject:smartMoveButton];
    }

    // Create original buttons with spacing
    tag = 0; // Start original button tags from 0
    for (int i = 1; i <= 8; i++) {
        CGFloat offset = (i - 1) * (buttonWidth + spacing);
        UIButton *originalButton = [self createButtonWithTitle:[NSString stringWithFormat:@"B%d", i]
                                                          frame:CGRectMake(offset, buttonHeight, buttonWidth, buttonHeight)
                                                            tag:tag++];
        [self.buttons addObject:originalButton];
    }
}

- (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame tag:(NSInteger)tag {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [self configureButton:button];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
    
    // Assign tag to button
    button.tag = tag;
    
    [self addSubview:button];
    return button;
}

- (void)configureButton:(UIButton *)button {
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    button.titleLabel.numberOfLines = 4;
    // Fixes label position
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    button.backgroundColor = self.backgroundColor;
}

Everything is visible but I cannot click on any buttons.

Can anyone see what’s wrong here?

In my old project, which was working fine many years ago I did this instead:

    UIViewController *rootViewController = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
// And in quickMove I would add the buttons via:
[rootViewController.view addSubview:button];

This approach no longer works because it’s deprecated(?) Nothing at all is visible if I try this.

0

There are 0 best solutions below