Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton is not Highlighting within ViewController in UIPageViewController in iOS7

I have a UIButton within a view controller which is embedded in a UIPageViewController. Page view controller is in scrolling mode(So No gestures ). When I touch on the button, action method will be called but there is no visible state changes (Highlighted state) is happening for the button. That means button looks like untouched. Observed only in iOS7. Any work around for this?

Case 1 : In Normal ViewController the behaviour of Button with and without Tap

Before tappingAfter tapping

Case 2 : In one of the PageViewController's viewController the behaviour of Button with and without Tap

Before tappingenter image description here

like image 450
Anil Varghese Avatar asked Feb 28 '14 10:02

Anil Varghese


People also ask

How do you highlight a button when it is clicked Swift?

In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button. You can change to any color you want.

What is page view controller in iOS?

The PageViewController is used in many of the iOS applications to let the user navigate between the various pages of the content of the application. The navigation can be controlled programmatically in the application. The PageViewController uses the transition that we specify to animate the change.


2 Answers

This usually happens when you put a button in a UIScrollView. UIScrollView has a property called "dalaysContentTouches" which defaults to YES. If it is YES, then the touch down on the button will be delayed (to give time to check if it's a scroll gesture), so you won't see the highlight happen. Still, though, if you tap it, it will register as a button press, without ever becoming highlighted.

I'm not positive what your setup is exactly, but it seems like a similar issue. Check any gestureRecognizers involved in your UI and check the values for "delaysTouchesBegan" and "delaysTouchesEnded".

**edited after experimentation**

I just tried this out on iOS 7.0 and it seemed to do the trick.

-(void)viewDidAppear:(BOOL)animated {
    NSArray *subviews = self.pageViewController.view.subviews;
    NSArray *viewHierarchy = [@[self.pageViewController.view] arrayByAddingObjectsFromArray:subviews];
    int i = 0;
    for (UIView *viewToCheck in viewHierarchy) {
        for (UIGestureRecognizer *gestureRecognizer in viewToCheck.gestureRecognizers) {
            NSLog(@"%d gestureRecognizer: %@", i++, gestureRecognizer);
            gestureRecognizer.delaysTouchesBegan = NO;
        }
    }
}

I get this output

0 gestureRecognizer: <UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x8e9b510; state = Possible; delaysTouchesBegan = YES; view = <_UIQueuingScrollView 0x9b7f800>; target= <(action=delayed:, target=<_UIQueuingScrollView 0x9b7f800>)>>
1 gestureRecognizer: <UIScrollViewPanGestureRecognizer: 0x8e9ba10; state = Possible; delaysTouchesEnded = NO; view = <_UIQueuingScrollView 0x9b7f800>; target= <(action=handlePan:, target=<_UIQueuingScrollView 0x9b7f800>)>; must-fail = {
        <UIScrollViewPagingSwipeGestureRecognizer: 0x8e9bec0; state = Possible; view = <_UIQueuingScrollView 0x9b7f800>; target= <(action=_handleSwipe:, target=<_UIQueuingScrollView 0x9b7f800>)>>
    }>
2 gestureRecognizer: <UIScrollViewPagingSwipeGestureRecognizer: 0x8e9bec0; state = Possible; view = <_UIQueuingScrollView 0x9b7f800>; target= <(action=_handleSwipe:, target=<_UIQueuingScrollView 0x9b7f800>)>; must-fail-for = {
        <UIScrollViewPanGestureRecognizer: 0x8e9ba10; state = Possible; delaysTouchesEnded = NO; view = <_UIQueuingScrollView 0x9b7f800>; target= <(action=handlePan:, target=<_UIQueuingScrollView 0x9b7f800>)>>
    }>

which reveals a UIScrollViewDelayedTouchesBeganGestureRecognizer, which seems to be a custom recognizer for the PageController designed to do exactly that to your button...

But! Set its delaysTouchesBegan property to NO, and the button highlight appears immediately on press. Hope that works for you too.

like image 61
devdavid Avatar answered Nov 01 '22 14:11

devdavid


  • check your UIButton states defined in xib or storyboard (but since you said it's only in iOS7, shouldn't be a problem)

  • check, which State your Button has after click in your IBAction method

  • try to set your recommend Button state in the IBAction method

    yourButton.higlighted = YES;
    
    //or yourButton.selected = YES; setting selected state 
    
like image 1
longi Avatar answered Nov 01 '22 14:11

longi