iOS 由ScrollView中的子视图View向下偏移64个像

作者: 逍遥晨旭 | 来源:发表于2017-02-20 17:46 被阅读936次

    问题:

    UINavigationController设置为self.window的根视图,然后将UIVIewController设置为UINavigtionController的根控制器。在UIViewController中加入一个ScrollView,再ScrollView中加入一个view。运行发现.:ScrollView显示正常,而 ScrollView中的子视图VIew向下偏移了64个像素。

    截图.png

    解决方法:

    设置UIVIewController的automaticallyAdjustsScrollViewInsets属性

      self.automaticallyAdjustsScrollViewInsets = NO;
    

    在解决这个问题的过程中,尝试了好几个属性的设置,在此一并记录一下作用:

    @property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES
    @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
    @property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0); // Defaults to NO, but bars are translucent by default on 7_0.  
    

    1、首先我们来说说:automaticallyAdjustsScrollViewInsets这个属性,默认值为YES.

    这个属性的官方解释:

    A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets

    Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

    如果是UIScroolview以及继承自它的控件,默认值YES是设置它的Insets为自适应。这里自适应其实就是空出导航栏的位置。

    PS:这个属性不仅可以解决类似于开篇的问题。也对直接添加到viewcontroller上的控件起作用。

    当 automaticallyAdjustsScrollViewInsets 为 NO 时,tableview 是从屏幕的最上边开始,也就是被导航栏和状态栏覆盖。如下图:

    贴边.png

    当 automaticallyAdjustsScrollViewInsets 为 YES 时,也是默认行为,表现就比较正常了。

    不贴边.png

    2、edgesForExtendedLayout

    它是一个类型为UIExtendedEdge的属性,指定边缘要延伸的方向,edgesForExtendedLayout的默认值很自然地是UIRectEdgeAll,四周边缘均延伸,就是说,如果视图中有navigationBar,下有tabBar,那么视图仍会延伸覆盖到四周的区域。

    PS:这个属性只对直接添加到viewcontroller上的控件都起作用,但不适用于开篇那样的问题。

    UIRectEdgeAll.png

    edgesForExtendedLayout设置成UIRectEdgeNone截图如下:

    UIRectEdgeNone.png

    导航栏就变成灰色的了,此时只需设置一下

    self.navigationController.navigationBar.translucent = NO;
    

    显示就完全正常了。edgesForExtendedLayout 为UIRectEdgeNone时,效果跟automaticallyAdjustsScrollViewInsets 为 YES 时一样。

    3、extendedLayoutIncludesOpaqueBars

    英文解释:

    So, if you extend your view to cover the navigation bar (edgesForExtendedLayout toUIRectEdgeAll) and the parameter is NO (default) it wont cover the status bar if it's opaque.

    If something is not clear, write a comment and I'll answer to it.

    • How iOS knows what UIScrollView to use? *iOS grabs the first subview in your viewcontroller's view, so the one at index 0, and if it's a subclass ofUIScrollView then applies the explained properties to it.

    Of course, this means that UITableViewController works by default (since theUITableView is the first view).

    默认值为NO,这个属性在状态栏不透明的情况下才生效。如果状态栏是不透明的,那么页面的布局默认是不会包含状态栏的,除非将这个属性设置成为YES。

    但是我怎么设置效果都不明显,有待进一步研究。

    Behind every beautiful thing, there's some kind of pain.####

    美丽背后,必有某种努力。####

    相关文章

      网友评论

      • 独木舟的木:如果在UIScrollView中放两个左右位置的子VIew的话,好像还会有点问题。
        逍遥晨旭:@独木舟的木 我试了一下,可能是界面太简单,不用设置任何属性都是正常的,你看看是不是frame的问题,或其他的原因影响到它了。
        独木舟的木:@此生逍遥
        self.automaticallyAdjustsScrollViewInsets = YES;时,左边的View1是正常显示的,右边的View2是向下偏移64点;
        self.automaticallyAdjustsScrollViewInsets = NO;时,右边的View2是正常显示的,左边的View1是向上偏移64点;
        逍遥晨旭:还会有电什么问题呢!可以一块探讨一下。
      • Cyyyyyyyy:这个问题是在UIScrollView是它的父View的第一个View的时候出现。
        我尝试设置过automaticallyAdjustsScrollViewInsets标记但不起作用。最后是在父View里先加一个空view,再加UIScrollView搞定,供参考。
        [self.view addSubview:[UIView alloc] init];
        [self.view addSubview:self.scrollView];
        逍遥晨旭:能解决问题就是好方法!!!

      本文标题:iOS 由ScrollView中的子视图View向下偏移64个像

      本文链接:https://www.haomeiwen.com/subject/myxewttx.html