Unwanted blank space to the left of UIScrollView on iPhone X

UIScrollView works fine with no space left on all iPads or iPhones except iPhone X. How can I remove a space?

I use storyboards. Bounce On Scroll / Zoom disabled. There is no white space on the iPad or iPhone except the iPhone X. I think it could be something related to security.

enter image description here

+8
background-color ios uiscrollview iphone-x safearealayoutguide
source share
4 answers

Well, I did not solve this problem in an elegant way. But it works like a charm. (I tried all the other answers. Thanks for your help, however these answers do not 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: ... ) 
0
source share

Setting limits on safeArea is good practice for iPhone-X. So the apple says -

When a view is visible on the screen, this guide reflects a portion of the view that is not covered by navigation bars, tabs, toolbars, and other ancestors.

In your case, you give the leading and trailing of scrollView with safeArea , Not superView

Therefore, if you take the restriction of providing superView superView instead of safeArea , your object content may be truncated, especially when you turn left, the content on the left will be more pinched under the top iPhone-X label.

enter image description here

Apple doc for safeAreaLayoutGuide

+3
source share

This is the distance from the safe area that applies to the left / right margin of UIScrollview as pasting content in landscape orientation on iPhone X, which can be seen using the read-only property of UIScrollview.safeAreaInsets .

The following line can be used to get rid of safe area attachments when you don't need it:

UIScrollview.contentInsetAdjustmentBehavior = .never

The default value, equal to UIScrollViewContentInsetAdjustmentBehavior.automatic , includes safe fields for guiding how to plan an area as an insertion of content.

Note. The limitations of the automatic layout have nothing to do with inserts; its behavior is in the settings for inserting IIS 11 UIScrollview content.

0
source share

The "safe area" is responsible for this empty space.

1st option:
Ignore the safe layout for your scrollview and set scrolling restrictions for its superview (or main view). Scrollview automatically processes the safe area tab for content while scrolling.

Landscape:

enter image description here

Portrait view:

enter image description here


2nd option:
I do not recommend deleting / changing the location of the safe area to view scrolling and an alternative solution that can solve the problem of white space visibility:

  • Set the blue background color (which you used to view the scroll) to the main view of your view controller if the scroll view covers the entire screen.
  • set a transparent color background to view the scroll.

Add this code to 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 some good help answers regarding a safe area plan for a better understanding:

  • Xcode 9 Safe Zone
  • Programmatically Use Safe Zone Configuration
-one
source share

All Articles