Johnny Rockex
Johnny Rockex

Reputation: 4196

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

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?

Upvotes: 1

Views: 1813

Answers (2)

jack moseley
jack moseley

Reputation: 429

Try setting contentInsetAdjustmentBehavior on your UIScrollView to .never


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

Upvotes: 4

kalpesh satasiya
kalpesh satasiya

Reputation: 817

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

}

Upvotes: 2

Related Questions