不是UIView子视图的按钮不起作用

时间:2012-11-27 01:10:08

标签: objective-c ios cocoa-touch

如果我将其更改为timerView的子视图,它可以正常工作,但没有其他工作。

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 33)];
[self.view addSubview:timerView];

UIImageView *changerBackground = [[UIImageView alloc] init];
[changerBackground setImage:[UIImage imageNamed:@"speedbackground.png"]];
changerBackground.frame = CGRectMake(-12 , -16, 105, 33);
[timerView addSubview:changerBackground];

UIButton *normal = [UIButton buttonWithType:UIButtonTypeCustom];
normal.frame = CGRectMake(40, 8, 22, 22);
[normal setBackgroundImage:[UIImage imageNamed:@"normalwhite.png"] forState:UIControlStateNormal];
[normal setBackgroundImage:[UIImage imageNamed:@"normalred.png"] forState:UIControlStateHighlighted];
[normal addTarget:self action:@selector(quickTest:) forControlEvents:UIControlEventTouchUpInside];
[changerBackground addSubview:normal];

1 个答案:

答案 0 :(得分:1)

[changerBackground setUserInteractionEnabled:YES];

将解决您的问题。由于UIImageView默认禁用用户交互。