0

I'm seeing strange behaviour for a scrollview that I'm trying to horizontally centre on the device.

I've a scrollview, set up in viewDidLoad()

scrollView = UIScrollView()
scrollView.delegate = self
scrollView.minimumZoomScale = 0.
scrollView.maximumZoomScale = 2.0
scrollView.zoomScale = 1.0
scrollView.contentSize = CGSize(width: width, height: height) 

and I've added a containerView for my content. I changed the background color here to try to be able to see what is going on

containerView = UIView()
containerView.backgroundColor = .black
scrollView.addSubview(containerView)
view.addSubview(scrollView)

To test this, I decided to set up a leading constant for the containerView (I'll centre it later).

containerView.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 50))

The view does seem to move 50 to the right. However 1) I lose the background color of containerView 2) containerView no longer seems to react to gestures

I thought I'd be smarter, and add the constraint to the scrollView instead - I keep the color and the gestures but it doesn't shift to the right.

1
  • You also can use Cartography, to write constrains more easily and readable. Commented Mar 12, 2017 at 10:49

2 Answers 2

1

constraints between scrollView and view controller view

 NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
            scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
        ])

constraints between scrollView and containerView

 NSLayoutConstraint.activate([
        containerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
        containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
        containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 50),
        containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)
    ])

Hope is helps!

Sign up to request clarification or add additional context in comments.

Comments

0

I have the almost the same issue before with scrollView. Try landing the container using only CGRect like you did with the contentSize. In this case, translatesAutoresizingMaskConstraint should be true.

2 Comments

I'm sorry I don't understand what you mean by landing the container
I mean not use constraint in your containerView view. If you want use constraint, use constraint in your scrollView Too. In my case, I did without constraint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.