Banana
Banana

Reputation: 4228

UIButton not highlighted on border

I create a UIButton programmatically using the following code:

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

Now the problem I am having is with highlighting of the button when it is tapped. It works well, i.e. the button color changes on tap, except when I click at the very border of the left side of the button. To illustrate this:

enter image description here

When the green part of the button is clicked, everything is fine and the button is highlighted. When the red part is clicked the button's selector is invoked, i.e. the button works, but the highlighting does not work.

I tested this on the simulator to be able to make more precise taps.

As for positioning the button, I set it's constraints programmatically so that it is actually outside of its parent view. The parent view has an attached gesture recognizer and I was playing with it trying to see whether the recognizer interferes with the button, but no UIGestureRecognizerDelegate methods of parent view are invoked when the button is tapped.

Any idea what might be the issue here?

Upvotes: 3

Views: 667

Answers (2)

Banana
Banana

Reputation: 4228

Finally I realised that the problem was a UIScreenEdgePanGestureRecognizer that I have. Since my button is positioned to the left of the screen, this small left part of the button was canceled by the UIScreenEdgePanGestureRecognizer. I solved this by adding the following code to my UIViewController:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

Upvotes: 2

Santu C
Santu C

Reputation: 2654

I don't think this issue can be detectable with actual device . But you may try below code for button border highlighting -

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

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

don't forgot to import below framework -

 #import <QuartzCore/QuartzCore.h>

Upvotes: 0

Related Questions