在设置了导航栏 [self.navigationController.navigationBar setTranslucent:YES];的情况下,如果添加了UIScrollerView,即使这时候设置了frame为(0,0,**,**)也会下移,空出导航栏的位置,原因是scrollerview下UIScrollViewContentInsetAdjustmentBehavior的属性使scrollerview发生了偏移:
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
*/
@property(nonatomic)UIScrollViewContentInsetAdjustmentBehaviorcontentInsetAdjustmentBehaviorAPI_AVAILABLE(ios(11.0),tvos(11.0));
默认的属性为 UIScrollViewContentInsetAdjustmentAutomatic
点击进入之后可以看到:
typedefNS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic,// Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable
UIScrollViewContentInsetAdjustmentScrollableAxes,// Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever,// contentInset is not adjusted
UIScrollViewContentInsetAdjustmentAlways,// contentInset is always adjusted by the scroll view's safeAreaInsets
}API_AVAILABLE(ios(11.0),tvos(11.0))
那么问题也就简单了,我们只需要设置我们的UIScrollView的 scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;就可以解决偏移的问题了:
网友评论