我在self.view
中创建了一个menuView,menuView有两个子视图,它们是UIView。
这个UIView包含4个UIButton,但是我无法点击任何一个。 userInteractionEnabled
在视图调试中设置YES,按钮的框架设置正确。
代码:
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
leftView.userInteractionEnabled=YES;
self.leftView=leftView;
[self.view addSubview:leftView];
//nightButton
UIButton *nightButton=[self.leftView viewWithTag:6];
[nightButton addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
leftView.userInteractionEnabled=YES;
//set
UIButton *settingButton=[self.leftView viewWithTag:4];
[settingButton addTarget:self.leftView action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
}
** LeftMenuView.m:**这是一个UIView类。这是我设置按钮和菜单的地方。
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
self.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
[self addHeaderView];
[self addButton];
return self;
}
#pragma mark -add view to menu view
-(void)addHeaderView
{
UIView *headViewUp=[[UIView alloc]init];
headViewUp.userInteractionEnabled=YES;
headViewUp.frame=CGRectMake(0, 0, yScreenWidth/2, 114);
headViewUp.backgroundColor=ColorWithRGB(26, 31, 36);
headViewUp.backgroundColor=[UIColor redColor];
self.headViewUp=headViewUp;
[self addSubview:headViewUp];
UIView *headViewDown=[[UIView alloc]init];
headViewDown.userInteractionEnabled=YES;
headViewDown.frame=CGRectMake(0, yScreenHeight-50, yScreenWidth/2, 50);
headViewDown.backgroundColor=ColorWithRGB(26, 31, 36);
self.headViewDown=headViewDown;
[self addSubview:headViewDown];
}
-(void)addButton
{
UIButton *settingButton=[self setFrame:CGRectMake(yScreenWidth*2/6, 64, yScreenWidth/6, 50 ) title:@"设置" image:@"Menu_Icon_Setting"];
settingButton.userInteractionEnabled=YES;
settingButton.tag=4;
[self resizeTextAndImageInButton:settingButton];
[self.headViewUp addSubview:settingButton];
UIButton *nightButton=[self setFrame:CGRectMake(yScreenWidth/4, 0, yScreenWidth/4, 50 ) title:@"白天" image:@"Menu_Day"];
nightButton.tag=6;
[self.headViewDown addSubview:nightButton];
}
-(UIButton *)setFrame:(CGRect)frame title:(NSString *)title image:(NSString *)imageName
{
UIButton *button=[[UIButton alloc]initWithFrame:frame];
[button setTitle:title forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:8];
button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
return button;
}
-(void)resizeTextAndImageInButton:(UIButton *)button
{
[button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,-button.imageView.frame.size.width, 0.0,0.0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -button.titleLabel.bounds.size.width)];
}
clilk按钮的方法不起作用。
答案 0 :(得分:4)
我查看了你的代码。你犯了一个错误,你没有设置“LefMenuView”的框架。即使你没有这样做,它的子视图是可见的,但不可触摸或可点击。
试试这个。我通过添加下面的代码运行。现在我能够看到点击操作。
Rule of thumb : if super view frame is Zero then all subviews are visible but not touchable.(Because you are touching out side of bounds).
ViewController.m
LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
leftView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
leftView.userInteractionEnabled=YES;
答案 1 :(得分:1)
在您的代码中设置目标时,您遇到了错误。 请这样做
[settingButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
设置settingButton.userInteractionEnabled=YES;
现在处理点击事件,如下所示
-(void)buttonPressed:(id) sender
{
UIButton *btn=(UIButton*) sender;
}
答案 2 :(得分:0)
您需要使用相应的方法
为按钮添加目标[button addTarget:self
action:@selector(aButtonMethod:)
forControlEvents:UIControlEventTouchUpInside];
此代码需要以下
-(void)addButton
{
UIButton *settingButton=[self setFrame:CGRectMake(yScreenWidth*2/6, 64, yScreenWidth/6, 50 ) title:@"设置" image:@"Menu_Icon_Setting"];
settingButton.userInteractionEnabled=YES;
settingButton.tag=4;
[self resizeTextAndImageInButton:settingButton];
[self.headViewUp addSubview:settingButton];
UIButton *nightButton=[self setFrame:CGRectMake(yScreenWidth/4, 0, yScreenWidth/4, 50 ) title:@"白天" image:@"Menu_Day"];
nightButton.tag=6;
**[nightButton addTarget:self
action:@selector(aButtonMethod:)
forControlEvents:UIControlEventTouchUpInside];**
[self.headViewDown addSubview:nightButton];
}
或
- (void)viewDidLoad {
[super viewDidLoad];
LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
leftView.userInteractionEnabled=YES;
self.leftView=leftView;
[self.view addSubview:leftView];
//nightButton
UIButton *nightButton=[self.leftView viewWithTag:6];
[nightButton addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
leftView.userInteractionEnabled=YES;
[nightButton addtoSubView:self.view]
//set
UIButton *settingButton=[self.leftView viewWithTag:4];
[settingButton addTarget:self.leftView action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
[settingButton addtoSubView:self.view]
}
你基本上创建了两次相同的按钮,但只有一组添加一个目标而另一组你没有将它们添加到视图中