In IOS11 after the system, due to the Apple phone more than a bangs, for the screen adaptation of UIScrollView and its subclasses of controls, then more than a need to pay attention to the point, while free today, Review and organize the scrollView contentInsetAdjustmentBehavior this attribute.

Balance:

To see what the contentInsetAdjustmentBehavior is

The enumeration definition looks like this:

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
    UIScrollViewContentInsetAdjustmentAutomatic,
    UIScrollViewContentInsetAdjustmentScrollableAxes,
    UIScrollViewContentInsetAdjustmentNever,
    UIScrollViewContentInsetAdjustmentAlways, 
}
Copy the code

Automatic, Automatic judgment need adapter safety area, when the controller of automaticallyAdjustsScrollViewInsets = YES, will fit the safety area, NO, will not fit safety area.

ScrollableAxes: This is a little hard to understand, when the contentSize width/height > frame. The size, the width/height, or, scrollView. AlwaysBounceHorizontal/Vertical = YES, Fit a safe distance.

Never: not applicable to security zones.

Always: Always fit into a safe zone, no matter what the situation.

Here’s an example:

/ / create a scrollView, add a picture the self. The view. The backgroundColor = [UIColor whiteColor]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: self.view.bounds]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; UIImage *testImg = [UIImage imageNamed: @"img"]; UIImageView *imageView = [[UIImageView alloc] initWithImage: testImg]; [self.view addSubview: scrollView];Copy the code

A, UIScrollViewContentInsetAdjustmentAutomatic

Automatically adjust whether a safety distance needs to be adapted

CGRect frame = imageView.frame;
frame.size.width = 390;
frame.size.height = 588;
imageView.frame = frame;
    
[scrollView addSubview: imageView];
scrollView.contentSize = imageView.frame.size;
scrollView.contentInsetAdjustmentBehavior =  UIScrollViewContentInsetAdjustmentAutomatic;
Copy the code

When: the controller of automaticallyAdjustsScrollViewInsets = YES, adapter safety area: But what we found is that in landscapeThe bangs didn’t fit“Automatic = =

Here’s a configuration:

scrollView.alwaysBounceHorizontal = YES;Solve!When: the controller of automaticallyAdjustsScrollViewInsets = NO, do not fit safety area

Second, the UIScrollViewContentInsetAdjustmentScrollableAxes

There are several cases of this pattern

Case 1: The ContentSize of the scrollView is too small to fit the security zone.

Case 2: scrollView ContentSize is small, but alwaysBounceHorizontal = YES, alwaysBounceVertical = YES, then fit the security zone.

Case 3: The scrollView ContentSize is larger than the display range, then fit the security zone.

I won’t paste the picture, it’s quite understandable.

The other two model UIScrollViewContentInsetAdjustmentNever and UIScrollViewcontentInsetAdjustmentAlways don’t speak is the literal meaning.

Finally, a demo is attached to help understand