Unwanted white space on left of UIScrollView on iPhone X
Asked Answered
G

4

14

UIScrollView works fine without the white space on the left on all iPads or iPhones except for iPhone X. How can I remove the white space?

I use storyboards. Bounce On Scroll/Zoom are all disabled. No white space on iPad or iPhone except for iPhone X. I think it might be something related to the Safe Area thing.

enter image description here

Gerson answered 22/10, 2017 at 23:49 Comment(4)
are you using storyboards? have you looked at safe areas?Dys
I use storyboard. Bounce On Scroll/Zoom are all disabled. No white space on iPad or iPhone except for iPhone X. I think it might be something relating to the Safe Area thing...Gerson
If you set the constraint to go past the safe area and to the end of the super view then it might work but you really haven’t given enough info so it could be many thingsDys
Share your code and storyboard design. So I can provide you complete solution.Turnkey
G
1

Well, I solved this issue in non-elegant way. But it works like a charm. (I tried all other answers. Thank for your help, however those answers don't seem to work in my case.)

var leftMargin: CGFloat = 0
var rightMargin: CGFloat = 0

if Device.isPhone() && Device.IS_5_8_INCHES() {
    self.leftMargin = 44
    self.rightMargin = 44
}

let frame = CGRect(
    x: (self.view.frame.width - self.leftMargin - self.rightMargin) * CGFloat(pageIndex),
    y: ...
)
Gerson answered 11/2, 2018 at 22:57 Comment(0)
T
34

This spacing is from safe area, which is applied to left/right of UIScrollview as content insets in landscape orientation on iPhone X, which can be seen using read-only property UIScrollview.safeAreaInsets.

Following line can be used to get rid of safe area insets when you dont need:

UIScrollview.contentInsetAdjustmentBehavior = .never

The default value being UIScrollViewContentInsetAdjustmentBehavior.automatic includes safe area layout guide margins as content insets.

Note: auto layout constraints has nothing to do with the insets, its just iOS 11 UIScrollview content insets adjustment behavior.

Trapezohedron answered 3/2, 2018 at 21:23 Comment(1)
Makes no difference to me. My images on my scrollview still are being squished inside the safe area and won't extend to the edges for all sides.Muzhik
A
5

Setting the constraint relative to safeArea is good practise for iPhone-X. This is how apple says -

When the view is visible onscreen, this guide reflects the portion of the view that is not covered by navigation bars, tab bars, toolbars, and other ancestor views.

In your case you are giving constraints leading & trailing of scrollView with safeArea, Not superView

Hence if you take risk giving constraint to superview instead of safeArea your object content may clipped, specially when you rotate left, content from the left most will clip under top notch of iPhone-X.

enter image description here

Apple doc for safeAreaLayoutGuide

Aprilaprile answered 23/10, 2017 at 4:18 Comment(4)
In iPhone-X, For prevent object clipped from corner, notch- iOS11+ comes with safeAreaAprilaprile
I forget select "Use Safe Area Layout Guides". However I am not able to get the the width of margin of the white bars to the left or right of the safe area.Gerson
@zsong, You can watch wwdc2017/204/ for more reference.Aprilaprile
@Trapezohedron see this for VFL #46479788Aprilaprile
G
1

Well, I solved this issue in non-elegant way. But it works like a charm. (I tried all other answers. Thank for your help, however those answers don't seem to work in my case.)

var leftMargin: CGFloat = 0
var rightMargin: CGFloat = 0

if Device.isPhone() && Device.IS_5_8_INCHES() {
    self.leftMargin = 44
    self.rightMargin = 44
}

let frame = CGRect(
    x: (self.view.frame.width - self.leftMargin - self.rightMargin) * CGFloat(pageIndex),
    y: ...
)
Gerson answered 11/2, 2018 at 22:57 Comment(0)
T
0

Safe Area Layout is responsible for this white space.

1st Option:
Ignore safe area layout for your scrollview and set scrollview's constraints with respect to its super view (or main view). Scrollview automatically handle safe area inset for contents while scrolling.

Landscape View:

enter image description here

Portrait View:

enter image description here


2nd Option:
I do not recommend to remove/change safe area layout for your scroll view and an alternate solution that can solve white space visibility issue:

  • Set blue background color (that you've applied for your scroll view) to your main view of your view controller, if scroll view is covering entire screen.
  • set clear color background for your scroll view

Add this code in your viewDidLoad

@ IBOutlet var scrollView: UIScrollView!

override func viewDidLoad(){
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.yellow // set here blue color that you've applied for your scroll view 

    self.scrollView.backgroundColor = UIColor.clear 
}

enter image description here

Here are good reference answers regarding Safe Area Layout, for better understanding:

Turnkey answered 23/10, 2017 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.