automaticallyAdjustsScrollViewInsets doesn't work in iOS9

余生颓废 提交于 2019-12-06 05:30:00

问题


In my app I have lots of VCs with scrollviews of all kinds (table, collection, etc.). All of them appear inside NavigationController and I used .automaticallyadjustsscrollviewinsets property of VC to prevent content from going under the NavigationBar. It works fine prior to iOS9.

I've updated my test iPhone to iOS9 yesterday and noticed that all scrollviews now does not have proper insets. I've updated XCode to latest version (7.0) and rebuilt the app - no effect. I've checked on the other device, which is still running iOS8.x - insets work correctly. I looked through iOS9 sdk changelogs and didn't find anything related to my problem. Is this a bug or a new behaviour? Anyone else having this problem? Possible solutions?

UPDATE1

I've came up with quick fix:

if (__iOS_9_OR_GREATER__) {

    CGFloat topInset = 20;

    if (self.navigationController) {
        topInset += self.navigationController.navigationBar.bounds.size.height;
    }

    UIEdgeInsets insets = UIEdgeInsetsMake(topInset, 0, 0, 0);

    self.collectionView.contentInset = insets;
    self.collectionView.scrollIndicatorInsets = insets;

}

It works. But question is still relevant, is this a bug in iOS9?

UPDATE2

Answering the question in the comments about __iOS_9_OR_GREATER__. Probably not the most efficient solution, but gets the job done.

#import <Foundation/Foundation.h>

#define __iOS_9_OR_GREATER__ [NKOSVersionCheck isMajorOSVersionAtLeast:9]

@interface NKOSVersionCheck : NSObject

+ (BOOL)isMajorOSVersionAtLeast:(NSUInteger)version;

@end

#import "NKOSVersionCheck.h"

@implementation NKOSVersionCheck

NSOperatingSystemVersion _majorOSVersion(int version) {

    NSOperatingSystemVersion iOS_x;
    iOS_x.majorVersion = version;
    iOS_x.minorVersion = 0;
    iOS_x.patchVersion = 0;

    return iOS_x;

}

+ (BOOL)isMajorOSVersionAtLeast:(NSUInteger)version {

    return [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:_majorOSVersion(9)];

}


@end

回答1:


Since iOS9 automaticallyadjustsscrollviewinsets will not change UIScrollView's contentInsets if it's created programmatically.

  • a) You can use UITableViewController for auto adjustments.
  • b) Set automaticallyadjustsscrollviewinsets and adjust insets by yourself.


来源:https://stackoverflow.com/questions/32650109/automaticallyadjustsscrollviewinsets-doesnt-work-in-ios9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!