2

I have created a ScrollView and a ImageView:

let scrollView: UIScrollView = {
    let scroll = UIScrollView()
    scroll.backgroundColor = UIColor.red
    scroll.contentSize.height = 1234
    scroll.translatesAutoresizingMaskIntoConstraints = false
    return scroll
}()

let filmImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.backgroundColor = UIColor.blue
    imageView.clipsToBounds = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

And I have added these to my view with constraints, as followed:

self.view.addSubview(scrollView)
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true

scrollView.addSubview(filmImageView)
filmImageView.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
filmImageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
filmImageView.heightAnchor.constraint(equalToConstant: 350).isActive = true

Everything works, except, on the iPhone X only, the imageView doesn't sit to the top of the screen. See the image below. (I made the scrollview background colour red so you can see)

enter image description here

I have tried adding the code to my scrollView.

scroll.contentInset = UIEdgeInsets.zero
scroll.scrollIndicatorInsets = UIEdgeInsets.zero
scroll.contentOffset = CGPoint(x: 0.0, y: 0.0)

Along with:

self.automaticallyAdjustsScrollViewInsets = false

But it makes no change. I have been unable to find anything on the internet on how to get my image to sit to the top of the scrollview for the iPhone X. All other iPhone devices display this fine.

Nick89
  • 2,948
  • 10
  • 31
  • 50
  • That's how Apple recommends it. Here's the [layout guide](https://developer.apple.com/ios/update-apps-for-iphone-x/) for iPhone X. You can see there's gap below the "notch" and another along the bottom of the screen – Code Different Apr 30 '18 at 02:51

2 Answers2

1

for the scrollView:

setting contentInsets to "never" instead of "always" solved the problem for me

enter image description here

Bassant Ashraf
  • 1,531
  • 2
  • 16
  • 23
0

For the programatic/code solution:

scrollView.contentInsetAdjustmentBehavior = .never
Sudhanshu Vohra
  • 1,345
  • 2
  • 14
  • 21