iOS 11:二、UIScrollView偏移

本文详细解析了iOS中导航栏的translucent、automaticallyAdjustsScrollViewInsets等属性的作用及交互影响,并介绍了iOS11后的contentInsetAdjustmentBehavior属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、translucent

/*
 New behavior on iOS 7.
 Default is YES.
 You may force an opaque background by setting the property to NO.
 If the navigation bar has a custom background image, the default is inferred 
 from the alpha values of the image—YES if it has any pixel with alpha < 1.0
 If you send setTranslucent:YES to a bar with an opaque custom background image
 it will apply a system opacity less than 1.0 to the image.
 If you send setTranslucent:NO to a bar with a translucent custom background image
 it will provide an opaque background for the image using the bar's barTintColor if defined, or black
 for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
 */
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

iOS 7之后这个属性默认为YES;
1、属性设置YES时:
(1)、如果添加的第一个子视图为UIScrollView,UIScrollView的原点在(0,0),UIScrollView的子视图的原点在(0,64)点开始。
(2)、如果添加第一个子视图非UIScrollView,原点在(0,0)点开始。
2、属性设置为NO时:
(1)、添加的子视图的原点都在(0,64)点开始。

二、automaticallyAdjustsScrollViewInsets

@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets API_DEPRECATED("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); // Defaults to YES

该属性在iOS11以后不起作用了;在iOS7-iOS10之间,translucent属性设置为NO时,该属性没有效果,在translucent属性设置为YES的前提下:
1、automaticallyAdjustsScrollViewInsets设置为YES:
(1)、如果添加的第一个子视图为UIScrollView,UIScrollView的原点在(0,0),UIScrollView的子视图的原点在(0,64)点开始。
(2)、如果添加第一个子视图非UIScrollView,原点在(0,0)点开始。
2、automaticallyAdjustsScrollViewInsets设置为NO:
(1)、添加的子视图的原点都在(0,0)点开始。

三、edgesForExtendedLayout

@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll

该枚举如下几个值:

typedef NS_OPTIONS(NSUInteger, UIRectEdge) {
    UIRectEdgeNone   = 0,
    UIRectEdgeTop    = 1 << 0,
    UIRectEdgeLeft   = 1 << 1,
    UIRectEdgeBottom = 1 << 2,
    UIRectEdgeRight  = 1 << 3,
    UIRectEdgeAll    = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
} NS_ENUM_AVAILABLE_IOS(7_0);

在translucent属性设置为NO时,该属性没有效果。
在translucent属性设置为YES前提下:
1、edgesForExtendedLayout设置设置为UIRectEdgeNone时;
(1)、添加的子视图的原点都在(0,64)点开始。
2、edgesForExtendedLayout设置设置为UIRectEdgeAll时;
(1)、遵循其他属性设定结果。

四、contentInsetAdjustmentBehavior

/* Configure the behavior of adjustedContentInset.
 Default is UIScrollViewContentInsetAdjustmentAutomatic.
 */
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));

前面说了在iOS11上,automaticallyAdjustsScrollViewInsets已经不起作用了,替换的是该属性,设置如下:

if (@available(iOS 11.0, *)) {
	self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
	self.automaticallyAdjustsScrollViewInsets = NO;
}

iOS11上对滚动视图加入了self-sizeing,默认如果不去实现viewForHeaderInSection就不会调用heightForHeaderInSection,
网上好多都是描述如何适配iOS11的tableView的顶部偏移。但是我一直没有试出来,按照网上描述该问题解决如下:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

若有朋友有tableView顶部空白的demo,请留言给我,谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiaobukuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值