UIButton not clickable outside initial frame of an UIScrollView

前端 未结 2 1558
北恋
北恋 2021-02-20 16:16

I got stuck with the strange behavior of UIScrollView that I cannot click on the UIButton after I enlarge the contentSize of the UIScrollView.

What I wanted to do:

相关标签:
2条回答
  • 2021-02-20 16:33

    You can add a button say submit and another button as subview of the scrollview.Keep your another button hidden in start and call setContentsize. now when user clicks on submit button then set another button to visible and again call set content size.In this way you would be able to achieve what you want to.

    i have tried to generate scenario for you.I have tried this on view based application with storyboard without auto layout.

    so in story board do some thing like this:

    #import <UIKit/UIKit.h>
    

    @interface ALViewController : UIViewController

    @property (nonatomic, weak) IBOutlet UIScrollView *scroll;
    

    @property (nonatomic, weak) IBOutlet UIButton *btn1; @property (nonatomic, weak) IBOutlet UIButton *btn2;

    =========== implementation:

    #import "ALViewController.h" 
    

    @interface ALViewController ()

    @end

    @implementation ALViewController

    - (void)viewDidLoad 
    

    { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.btn2 setHidden:YES];

    [self adjustContentSize];
    

    }

    -(void)adjustContentSize { CGRect contentRect = CGRectZero; for (UIView *view in self.scroll.subviews) { if (![view isHidden]) { contentRect = CGRectUnion(contentRect, view.frame); } }

    self.scroll.contentSize = contentRect.size;
    

    }

    • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

    -(IBAction)btnClicked:(id)sender { UIButton *btn = (UIButton *)sender; if (btn.tag ==1) { [self.btn2 setHidden:FALSE]; [self adjustContentSize]; }

    }
    

    @end

    and in storyboard some thing like this:

    enter image description here @end

    for your information:view frame 0,0,768,1024 scroll frame: 0,0,768,1024 btn1 frame: 361, 753, 46, 30 btn2 frame: 351, 1032, 46, 30

    run your code : at first your scroll view would be unscrollable because we have set btw 2 to be hidden and set content size.As soon as you will be clicking on btn 1, see code we are making btn 2 visible again and calling set content size.

    Conclusion:

    the scrollview content size looks for its visible contain of which we can take benefit of.

    thanks and regards.

    0 讨论(0)
  • 2021-02-20 16:54

    I assume that the button is direct or indirect child of the contents view you load from the xib file.

    The problem is because you changed the scroll view's contentSize but did not change the frame of the contents view. Superviews participate in touch hit testing. You can place a subview outside of its superview bounds and by default the subview will not be clipped but touch events will not be delivered to it because the superview checks during hit testing that the touch is outside of its bounds and rejects the touch. See UIView hitTest:withEvent: for details.

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