休息时内容不正确的UIScrollview(-20 px,而不是0px)

时间:2018-01-18 11:09:08

标签: ios objective-c uiscrollview

我有一个UIScrollview,它在y轴上展开并调用这个委托方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    float yOff = scrollView.contentOffset.y;
    NSLog(@"y off is %f", yOff);
}

当scrollView移动到顶部(即yOff == 0)时,它实际上在大多数手机上休息为-20px,在iPhoneX上为-44px。如何关闭此行为,这样做有什么风险?

2 个答案:

答案 0 :(得分:2)

尝试在contentInsetAdjustmentBehavior上设置UIScrollView.never

if (@available(iOS 11.0, *)) {
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

答案 1 :(得分:2)

试试这个:

#define IPHONE4 (( [ [ UIScreen mainScreen ] bounds ].size.height == 480 ) ? 1 :0)
#define IPHONE5 (( [ [ UIScreen mainScreen ] bounds ].size.height == 568 ) ? 1 :0)
#define IPHONE6 (( [ [ UIScreen mainScreen ] bounds ].size.height == 667 ) ? 1 :0)
#define IPHONE6p (( [ [ UIScreen mainScreen ] bounds ].size.height == 736 ) ? 1 :0)
#define IPHONEX (( [ [ UIScreen mainScreen ] bounds ].size.height == 812 ) ? 1 :0)


-(void)scrollViewDidScroll:(UIScrollView *)scrollView {


    if (IPHONEX) {
        scrollView.contentOffset = CGPointMake(0,44);

    }else{
        scrollView.contentOffset = CGPointMake(0,20);
    }

}