Rue Vitale
Rue Vitale

Reputation: 1899

how to center the scroll view inside the View

I have a scroll view added to the View Controller's View. On iPhone XR, the scroll view subviews appear to the left because the scrollView does not expand its parent's view and is not centered. How can I make the scroll view take the full width and height of its parent's view and center it?

Upvotes: 0

Views: 176

Answers (1)

Ruchi Makadia
Ruchi Makadia

Reputation: 1113

add below constraint to scrollview

 scrollview.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(scrollview)
    scrollview.topAnchor.constraint(equalTo: self.view.topAnchor, constant : 0).isActive = true
    scrollview.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant : 0).isActive = true
    scrollview.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant :0).isActive = true
    scrollview.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 0).isActive = true
    scrollview.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true

Upvotes: 1

Related Questions