ios - 无法单击 UIView 中的按钮

标签 ios objective-c uiview

我在 self.view 中创建了一个 menuView ,menuView 有两个 subview ,即 UIView。
此 UIView 包含 4 个 UIButton ,但我无法单击其中任何一个。 userInteractionEnabled设置为 YES,按钮的框架在 View 调试中设置正确。
代码:

更新: Project In Github

View Controller .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)];
}

单击按钮的方法不起作用。

最佳答案

我浏览了你的代码。您犯了一个错误,即您没有为“LefMenuView”设置框架。即使您没有这样做,它的 subview 也是可见的,但不可触摸或不可点击。

所以试试这个。我通过添加以下代码来运行。现在我可以看到点击 Action 了。

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).

View Controller .m
LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
leftView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
leftView.userInteractionEnabled=YES;

关于ios - 无法单击 UIView 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585087/

相关文章:

ios - 带有链接回 View Controller 的按钮的自定义对象

ios - 谷歌分析 SDK iOS10

ios - 登录后切换 View Controller ......最佳实践

iphone - 按比例缩放整个 UIView

android - 使用 ionic 框架从手机摄像头流式传输视频

ios - 动画 UICollectionView 页脚高度

ios - Objective-C 索引数组

swift - 一旦 UIView 出现在屏幕上就开始动画

ios - UICollectionViewCell Selection 选择/突出显示每 5 个单元格

ios - Swift 3,无法在 viewcontroller 中创建多个函数?