【问题标题】:Subview of custom UIButton class doesn't trigger click自定义 UIButton 类的子视图不会触发点击
【发布时间】:2012-12-11 13:27:13
【问题描述】:

我正在编写一个自定义 UIButton 子类,以创建我自己的完全自定义按钮 拥有完全的控制权。仍然,当我尝试添加子视图时遇到问题 到那个 UIButton 的视图。子视图阻止“触摸”事件,因此它不会冒泡 UIButton 本身..

这是一个演示: 我首先用一个框架创建了我的 CustomNavigationButton。它是洋红色的。 我可以在屏幕上看到洋红色,所以它就在那里。其次,我添加了一个子视图 到那个 CustomNavigationButton(它是绿色的),我可以看到绿色,所以它是 那里,但如果我点击绿色矩形(子视图),“UIControlEventTouchUpInside” 没有在我的 CustomNavigationButton 上被调用..

在我的 AppDelete 中:

CustomNavigationButton* mapBtn = [[CustomNavigationButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
mapBtn.backgroundColor = [UIColor magentaColor];
[mapBtn addTarget:self action:@selector(goMapHandler:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mapBtn];

这是我的 CustomNavigationButton 类(它是 UIButton 的子类)

@implementation CustomNavigationButton
@synthesize bg;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // STORE INITIAL FRAME
        self.initialFrame = frame;

        bg = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 40, 40)];
        bg.backgroundColor = [UIColor greenColor];
        [bg setUserInteractionEnabled:YES];
        [self addSubview:bg];       

    }
    return self;
}
@end

【问题讨论】:

    标签: ios uibutton subview user-interaction


    【解决方案1】:

    我想出了怎么做! 如果“bg”是我添加到 UIButton 的子视图,那么你应该这样做:

    bg.userInteractionEnabled = NO;
    bg.exclusiveTouch = NO;
    

    但请记住,如果您的子视图扩展了 UIButton 的框架, 触摸不会发生!您可以检查您的子视图是否超过 UIButton 通过给 UIButton 一个背景颜色。

    【讨论】:

    • 谢谢,我快疯了!
    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多