View at the bottom in a UIScrollView, with AutoLayout

前端 未结 2 1280
日久生厌
日久生厌 2020-12-08 05:20

I\'m setting up content in a scroll view with autolayout. The objects in the scrollview are pinned top-to-bottom to the previous one, so that they are under one another. I h

2条回答
  •  余生分开走
    2020-12-08 06:13

    you need to check ContentSize of scrollView and modify FooterView Top Constraint with the required Value

    My class code

    import UIKit
    
    class scrollViewDrag: UIViewController
    {
        /// ScrollView Outlet
        @IBOutlet weak var mainScrollView: UIScrollView!
    
        /// Footer View top spacing constraint
        @IBOutlet weak var footerViewTopConstraint: NSLayoutConstraint!
    
        /// Used for ScrollView Height
        var screenHeight = CGFloat()
    
        /// Did Load
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    
        /// Function used to check for height 
        func checkForHeight(){
            /// Get scrollView Height
            screenHeight = mainScrollView.frame.size.height
    
            /// Check contentSize Height ?
            if mainScrollView.contentSize.height >= screenHeight {
                /// When ScrollView is having height greater than your scrollView Height
                /// Footer will scroll along other Views
            }
            else{
                /// Issue Case
                let spacingValue = screenHeight-mainScrollView.contentSize.height
                footerViewTopConstraint.constant = spacingValue
            }
        }
    
        /// Call the height function in DidAppear
        override func viewDidAppear(_ animated: Bool) {
            checkForHeight()
        }
    }
    

    Storyboard

    I had used Four View with Equal Heights And at last a footerView is attached as Fourth View

    FooterView Top Constraint

    Top constraint used as footerViewTopConstraint

    Output

    Case 1 - Size is greater than scrollView Height

    Case 2 - Expected Output

提交回复
热议问题