yangOne
yangOne

Reputation: 89

Can't click button in UIView

I create a menuView in self.view,menuView has two subViews which are UIView. this UIView contains 4 UIButton ,but i can't click any one of it. userInteractionEnabled set YES, button's frame set correct in View Debugging. code:

UPDATE:Project In Github

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:**this is a UIView class. It is the place i set button and menu.

-(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)];
}

the method of clilk button doesn't work.

Upvotes: 1

Views: 7514

Answers (3)

Dhiru
Dhiru

Reputation: 3060

you are doing mistake While setting the target in you code . please do like this

[settingButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

set settingButton.userInteractionEnabled=YES;

Now handle the click event as below

-(void)buttonPressed:(id) sender
{

   UIButton *btn=(UIButton*) sender;


}

Upvotes: 1

MuraliMohan
MuraliMohan

Reputation: 1093

I went through your code. you are doing one mistake that you are not setting the frame for "LefMenuView".Even you did not do this, its subview are visible but not touchable or clickable.

So try this. i run by adding below code. Now i am able to see the click actions.

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;

Upvotes: 7

yawnobleix
yawnobleix

Reputation: 1342

You need to add a target for your button with the corresponding method

[button addTarget:self 
           action:@selector(aButtonMethod:)
 forControlEvents:UIControlEventTouchUpInside];

This code needs the following

-(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];
}

or

   - (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]
        }

you essentially create the same button twice but only for one set add a target and for the other set you don't add them to the view

Upvotes: 0

Related Questions