UIScrollview resting at incorrect contentOffset (-20 px, not 0px)

半城伤御伤魂 提交于 2021-01-28 08:25:00

问题


I have a UIScrollview that expands in the y-axis and calls this delegate method:

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

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

As the scrollView is moved to the top (ie. yOff == 0), it actually comes to rest at -20px on most phones and -44px on the iPhoneX. How do I switch this behaviour off, and what are the risks of doing so?


回答1:


Try setting contentInsetAdjustmentBehavior on your UIScrollView to .never


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



回答2:


Try this one:

#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);
    }

}


来源:https://stackoverflow.com/questions/48319873/uiscrollview-resting-at-incorrect-contentoffset-20-px-not-0px

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