简体   繁体   中英

UIButton not clickable when adding it programmatically

I'm creating custom UIView that contains two UILabel s and UIButton so I created a subclass to make this possible.

Now the process looks like:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];

    self.settingsButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 2 * margin, frame.size.height - titleHeight - dateHeight, buttonSize, buttonSize)];
    [self.settingsButton setImage:[UIImage imageNamed:@"settings_icon"] forState:UIControlStateNormal];
    [self addSubview:self.settingsButton];

    [self.settingsButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

Button appears on UIView but it is not clickable. I tried to set self.userInteractionEnabled = YES; and a couple things but still no result :(

This is the most dumb mistake I've ever made. Transparent UINavigationBar was blocking the touch.

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