user616076
user616076

Reputation: 4001

My ScrollView is not scrolling vertically

I'm writing an app in Xcode 9 with Swift 4 and I've added a UIScrollview to a view which is intended to show a jPeg which is 3030 pixels in height. I've added the scrollview to my view and assigned the delegate in Outlets in IB. I've attached the IBOutlet called scrollView to the UIScrollview and added a UIImageView with the jPeg to the UIScrollview. I've set the size of the UIScrollView to 375W and 620H and then set the UIImageView to 375W and 3030H. this should complete the work in Interface builder.

In the Controller I've added UIScrollViewDelegate to the Class and added the code below to ViewDidLoad

    //scrollView.delegate = self
    scrollView.contentSize = CGSize(width: 375, height: 3040)

I've commented out the delegate line as I've done that in IB. When I run the app, the screen comes upon with the image but when I try to scroll it barely scrolls more than one screen. What have I missed?

Upvotes: 4

Views: 13701

Answers (3)

Nischal Hada
Nischal Hada

Reputation: 3288

You can implement as below image screen shots

    //Height Constant of image
    @IBOutlet weak var ConstHeight: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()


        if #available(iOS 11.0, *) {
            self.scrollView.contentInsetAdjustmentBehavior = .never
        }else{
            self.automaticallyAdjustsScrollViewInsets = false

        }

        //You can change height on here too
         self.ConstHeight.constant = 800

    }

enter image description here enter image description here enter image description here

Upvotes: 1

Catherine
Catherine

Reputation: 682

You have to place your scroll view inside a UIView and another UIView inside your scroll view.

  1. Outer view Constraints: Leading,trailing,top and bottom - 0
  2. Scroll view Constraints: Leading,trailing,top and bottom - 0
  3. Inner view Constraints: Leading,trailling and top to Superview, Equal width to Outer view , Bottom space to -250 and Height equals 900(Height of your content)
  4. Set your contentsize now.

enter image description here

Upvotes: 2

user9106403
user9106403

Reputation:

You need to add a UIView inside your scrollview and then add that view top/bottom/leading/trailing 0 to scrollview then add your image view inside view and add image view bottom constraint to that view, So your scroll view will work properly.

Please refer below image for more details.

enter image description here

Upvotes: 3

Related Questions