为什么在UIImageView上添加button以后点击button不能执行
我在学习UI控件的使用
今天想实现在UIImageView 上面添加一个UIButton,点击button以后输出一段话
但是我加上去以后不能点击button。
不知道是哪里写错了
我是初学者,可能问题很菜。求大神指点下啊!
我把截图跟代码都贴出来啊。
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor yellowColor];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 140, 160, 80)];
//imgView.highlighted = NO;
imgView.backgroundColor = [UIColor greenColor];
imgView.image = [UIImage imageNamed:@"BB"];
[self.window addSubview:imgView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(80, 40, 80, 40);
// btn.selected = YES;
[btn setTitle:@"点我" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:btn];
// btn.enabled = YES;
[self.window makeKeyAndVisible];
return YES;
}
-(void)touchUpInside
{
NSLog(@"touchUpInside");
}
[解决办法]
路过,希望找到高手。
[解决办法]
因为UIImageView 默认是不接受Touch事件的
所以只要加上一句imgView.userInteractionEnabled = YES;