UIButton 未在边框上突出显示

UIButton not highlighted on border

我使用以下代码以编程方式创建一个 UIButton:

myButton = [UIButton new];
[myButton setImage:[UIImage imageNamed:@"myButtonNormal"]
                              forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"myButtonBackgroundNormal"]
                                        forState:UIControlStateNormal];
myButton.alpha = MY_BUTTON_ALPHA;

[myButton addTarget:self
             action:@selector(myButtonSelector:)
   forControlEvents:UIControlEventTouchUpInside];

[self.parentViewController.view insertSubview:myButton 
                                      atIndex:10];

现在我遇到的问题是点击按钮时突出显示该按钮。它运行良好,即按钮颜色在点击时发生变化,除非我单击按钮左侧的边界。为了说明这一点:

单击按钮的绿色部分时,一切正常,按钮高亮显示。单击红色部分时,将调用按钮的选择器,即按钮有效,但突出显示无效。

我在模拟器上对此进行了测试,以便能够进行更精确的点击。

至于按钮的定位,我以编程方式设置了它的约束,以便它实际上位于其父视图之外。父视图有一个附加的手势识别器,我正在玩它试图查看识别器是否干扰按钮,但是当点击按钮时没有调用父视图的 UIGestureRecognizerDelegate 方法。

知道这里可能有什么问题吗?

我认为实际设备无法检测到此问题。但是您可以尝试下面的代码来突出显示按钮边框 -

[myButton addTarget:self action:@selector(highlightButtonBorder:) forControlEvents:UIControlEventTouchDown];

- (void)highlightButtonBorder:(id)sender
{
    myButton.layer.borderColor = [[UIColor redColor]CGColor];
}

别忘了导入下面的框架 -

 #import <QuartzCore/QuartzCore.h>

最后我意识到问题出在我的 UIScreenEdgePanGestureRecognizer 上。由于我的按钮位于屏幕左侧,所以按钮的这个小左侧部分被 UIScreenEdgePanGestureRecognizer 取消了。我通过将以下代码添加到我的 UIViewController:

解决了这个问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}