Scrolling to content inside nested StackViews

suggest change

The big gotcha about scrolling is to determine the offset necessary to present (for instance) a Textfield inside a StackView with is inside the ScrollView.

If you try to get the position of Textfield.frame.minY can be 0, because the minY frame is only considering the distance between the element and the top of the StackView. So you have to consider all other parent stackviews/views.

A good workaround for this is:

1 - Implement the ScrollView Extension

extension UIScrollView {

    func scrollToShowView(view: UIView){
        var offset = view.frame.minY
        var superview = view.superview
        while((superview  != nil)){
            offset += (superview?.frame.minY)!
            superview = superview?.superview
        }
        
        offset -= 100 //optional margin added on offset
        
        self.contentOffset = CGPoint.init(x: 0, y: offset)
    }

}

This will consider all parent view and sum the necessary offset for the scrollview present the necessary view on screen (for example a Textfield which cannot stay behind the user keyboard)

Usage example:

func textViewDidBeginEditing(_ textView: UITextView) {
    self.contentScrollView.scrollToShowView(view: textView)
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


UIScrollView with StackView child:
* Scrolling to content inside nested StackViews

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
170 UIScrollView with StackView child
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable