简体   繁体   中英

UIButton inside scrollview not triggering action

I have a view called subADCview inside a scrollView, this subADCView generates button dynamically. Currently if the number of buttons doesnt fit in single line(ie in my subADCview which is about 320 width and 54 height) the view height expands and it becomes scrollable. And there is also provision to delete the buttons as well. Now if I have more number of buttons in multiple line and just simply delete them it works fine and there are no issues, however if I do some scrolling and then delete the buttons it works until the button fits to single line, soon as the view reduces to contain the buttons in single line the buttons inside doesnt trigger the action. Strange!! However it works if I dont scroll and just simply delete the buttons. I dont know what the issue is with the scroll, as soon as I do some scrolling and then delete the buttons it works until they fit on single line. I have used tap gestures and all, and the touch does fire the tap gesture method and returns "NO", but there is no sign button action getting triggered, it seems like the button is disabled but the log says its enabled. Below is my code with some logs. Appreciate if you can help.

//Tap gesture section

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{

    NSLog(@"touchview:%@,tag:%d",[touch.view class], touch.view.tag);

    if ([touch.view isKindOfClass:[UIButton class]])
    {
        UIButton *btn=(UIButton*)touch.view ;

        NSLog(@"Properties:%@,%hhd,%hhd",btn,btn.userInteractionEnabled,btn.enabled);

    }

    if (self.scrollView.superview != nil)
    {
        if ([touch.view isKindOfClass:[UIButton class]]||[touch.view.superview.superview isKindOfClass:[UITableViewCell class]]||[touch.view.superview.superview.superview isKindOfClass:[UITableViewCell class]]||[touch.view.superview isKindOfClass:[UITableViewCell class]])

        {
           NSLog(@"Value returned is NO");
            return NO; // ignore the touch
        }

    }


     NSLog(@"Value returned is YES");
    return YES; // handle the touch


}

//This is the function to trigger button action

 -(void)btnSelected:(UIButton *)button
    {
        NSLog(@"Button %ld Clicked",(long int)[button tag]);



        if (button.tag==0)
        {
            [self pushToGroupCategorySelection];
            for (id btn in self.subADCView.subviews)
            {
                if ([btn tag]!=-999)
                {
                    if ([btn isKindOfClass:[UIButton class]])
                    {
                        [btn removeFromSuperview]; //remove buttons
                    }
                }

            }
            _subADCView.frame=CGRectMake(_subADCView.frame.origin.x, _subADCView.frame.origin.y, _subADCView.frame.size.width, 54.0);

        }
        else
        {
            if (_categoryEditDetails.count==1)
            {
                showAlert(@"Message", @"Group Should Have Atleast 1 category", STRING_CONSTANT_OK,nil);
            }
            else
            {
                [categoryListDeleted addObject:[@{
                                                  @"id": [NSString stringWithFormat:@"%ld",(long)button.tag]

                                                  } mutableCopy]] ;

                for (id btn in self.subADCView.subviews)
                {
                    if ([btn tag]!=-999)
                    {
                        if ([btn isKindOfClass:[UIButton class]])
                        {
                            [btn removeFromSuperview]; //remove buttons
                        }
                    }

                }
                _subADCView.frame=CGRectMake(_subADCView.frame.origin.x, _subADCView.frame.origin.y, _subADCView.frame.size.width, 54.0);

                for (NSUInteger i=0;i<_categoryEditDetails.count;i++)
                {
                    if ([[[_categoryEditDetails objectAtIndex:i]valueForKey:@"id"] caseInsensitiveCompare:[NSString stringWithFormat:@"%ld",(long)button.tag]]==NSOrderedSame)
                    {
                        [_categoryEditDetails removeObjectAtIndex:i];
                        break;
                    }
                }
                [self addDynamicButtonsForCategories];
            }
        }
    }

This is the Log when it works when buttons are on single line before I did any scrolling

touchview:UIButton,tag:104
Properties:<UIButton: 0xcc99810; frame = (146 0; 134 40); opaque = NO; tag = 104; layer = <CALayer: 0xcca5290>>,1,1
Value returned is NO
Button 104 Clicked

This is the Log when it doesnt works when buttons are on single line after I did any scrolling

touchview:UIButton,tag:104
Properties:<UIButton: 0xd97f5d0; frame = (146 0; 134 40); opaque = NO; tag = 104; layer = <CALayer: 0xd97a810>>,1,1
Value returned is NO

Do notice that in both cases tap gesture worked and it recognizes the button touch but in second case the button action didnt get fired. I would really appreciate any solution to fix this issue or if you could give me more suggestions on detailed troubleshooting.

I wasnt able to find out the exact issue but I think it was related to the issue with view size and scrollview content size although in log everything seemed to be fine. Anyway I removed my subADCview and added the dynamic buttons directly in my scrollview and it worked great without any issues.

In XCode 5 with iOS 7 there is a bug where this problem can be solved by turning off AutoLayout.

Simply uncheck this box in your sidebar and you should be good to go.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM