Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buttons are disabled in a ScrollView - iOS Swift

I have a button in a view inside a view inside a ScrollView:

UIScrollView
-ContentView
--PhoneNumberView
---UIButton

If the scrolling ability of the ScrollView is disabled the button clicks normally. However, if the scrolling is enabled the button seems to be disabled.

This is how I sent the content size of the ScrollView:

var contentRect = CGRectZero;
for view in self.contentView.subviews {
     contentRect = CGRectUnion(contentRect, view.frame)
}
scrollView.contentSize = contentRect.size
scrollView.contentSize.height = scrollView.contentSize.height+20

and I also set those properties

scrollView.userInteractionEnabled = true
scrollView.exclusiveTouch = true
scrollView.canCancelContentTouches = true
scrollView.delaysContentTouches = true

and I finally tried to create a subclass of UIScrollView and I ended up with this:

class myScrollView: UIScrollView {

    override func touchesShouldCancelInContentView(view: UIView) -> Bool {
        return false
    }
}

but still the button is not active whenever there is any scrolling.

How can I fix that?

like image 342
Ali Alebrahim Avatar asked Jul 19 '16 09:07

Ali Alebrahim


2 Answers

Set the color of the ContentView & Scrollview. I think you are setting wrong contentsize. And your PhoneNumberView is outside the contentsize height.

EDIT

enter image description here

enter image description here

enter image description here

enter image description here

like image 94
Bhadresh Mulsaniya Avatar answered Oct 25 '22 18:10

Bhadresh Mulsaniya


By setting

scrollView.exclusiveTouch = true

you tell scrollView to handle touches exclusively, so touches won't reach its subviews, so set this property to false

This line of code

scrollView.delaysContentTouches = true

basically delays touches on scrollview subviews, if you want button to respond immediately then set this property to false too.

like image 45
Juri Noga Avatar answered Oct 25 '22 20:10

Juri Noga