【问题标题】:UIButton not firing selector if I draw it using CALayer如果我使用 CALayer 绘制 UIButton 不会触发选择器
【发布时间】:2010-04-27 22:25:21
【问题描述】:

大家好,我这样创建这个按钮:

_myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

    DLBadgeLayerDelegate *layerDelegate = [[DLBadgeLayerDelegate alloc] init];

    _myButton.frame = CGRectMake(0.0 , 0.0, 100.0, 100.0);
    [_myButton addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    _myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

    _myButton.center = CGPointMake(10.0, 10.0);
    [self addSubview:_myButton];
    [_myButton release];

该按钮出现在屏幕上,但如果我点击它,则没有任何反应。如果我只是创建一个圆角矩形按钮,而不是在其图层上没有自定义绘图,或者如果我只是将图层代码行留在外面,那么一切都很好。你有什么想法为什么我会出现这种行为,因为我想在按钮的图层中进行一些自定义绘图?

【问题讨论】:

    标签: iphone uibutton calayer uicontrol


    【解决方案1】:

    事件不会发送到 (1) 隐藏、(2) alpha

    编辑:尝试将背景颜色放在视图的背景颜色中,而不是图层的背景颜色中:

    _myButton.backgroundColor = [UIColor redColor];
    

    【讨论】:

      【解决方案2】:

      如果我不考虑这一行:

      _myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

      事件被正确触发,我仍然没有标题、alpha 等。我不认为这是问题或解释,但我也会尝试这种方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-08
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 2023-03-29
        • 2020-05-04
        相关资源
        最近更新 更多