UIButton does not work when it in UIScrollView

后端 未结 8 460
说谎
说谎 2020-12-01 10:38

My structure of views:

UITableView
  UITableViewCell
    UIScrollView
      CustomView
        UIButton

The problem is UIButton doesn\'t wo

相关标签:
8条回答
  • 2020-12-01 11:14

    Add all subviews from CustomView to ScrollView. And hide the CustomView. Make sure CustomView(ContainerView of scrollView) should also be a subView of scrollView.

    0 讨论(0)
  • 2020-12-01 11:17

    Create a subclass of UIScrollview with

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view {
      return NO;
    }
    

    If you are not getting that message, set:

    scrollView.delaysContentTouches = NO;
    

    The problem is because of the way touchesShouldCancelInContentView behaves by default, it returns NO only if the subview is an UIControl, but your button is hidden inside the CustomView which isn't.

    0 讨论(0)
  • 2020-12-01 11:17

    Link from Capt.Hook helped me. I used UIGestureRecognizer instead of UIButtons.

    Allow UIScrollView and its subviews to both respond to a touch Use it, if you have the similar problem.

    0 讨论(0)
  • 2020-12-01 11:19

    Your CustomView's frame could be interfering with your UIButton. Are they overlapping? Add your button to the CustomView just to test by [CustomView addSubview:UIButton]; (using your values of course). If it works, then your issue is most likely an overlap.

    0 讨论(0)
  • 2020-12-01 11:34

    I guess you need to set the button frame. By default if you use btn = [[UIButton alloc] init]; method to initialize button it will be UIButtonTypeCustom. Also set a background color to it for debugging purpose to note down where the button is actually placed. Now for your case you need to set the frame for that button like btn.frame = CGRectMake(0,0,100,100);

    For the inner UIScrollView implement this delegate method.

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view
      {
           return ![view isKindOfClass:[UIButton class]];
      }
    

    This may work.

    0 讨论(0)
  • 2020-12-01 11:37

    None of the answers worked for me, because my problem was that the button was not visible at layout time or something...

    FIX:

    1) move the button out of the scrollView completely (to be subview of VC's main view).

    2) select the button and the element from your contentV you want your button located (in my case: after the last element in conventV)

    3) add the necessary constraints (in my case: to be bottom aligned)

    4) Add the rest of the constraints for the button.

    5) Enjoy.

    0 讨论(0)
提交回复
热议问题