1

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?

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
  • 1
    This might help: https://stackoverflow.com/questions/18798792/explaining-difference-between-automaticallyadjustsscrollviewinsets-extendedlayo/18921109 – Ozgur Vatansever Jan 18 '18 at 11:11
  • Have you defined your scroll view in a view controller? Can you share your code with us? – Ozgur Vatansever Jan 18 '18 at 11:12
  • 1
    Actually your scrollview using top layout of VC, thats why it is started from -20px. If you check actually its x position is also displaced with same px. – dahiya_boy Jan 18 '18 at 11:13
  • @dahiya_boy yes, I see ensuring the uiscrollview does not originate from the top of the view controller view is a work around, ie. scrollview.frame = CGRectMake(0, 50, width, height-100), along those lines. – Johnny Rockex Jan 18 '18 at 11:16
  • please use safe area guides if you are supporting iPhone X – harsha yarabarla Jan 18 '18 at 11:17

2 Answers2

5

Try setting contentInsetAdjustmentBehavior on your UIScrollView to .never


if (@available(iOS 11.0, *)) {
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
jack moseley
  • 439
  • 4
  • 12
  • Or, if you want to keep your (automatic) content inset adjustment setting, get the adjusted value at runtime using `scrollview.adjustedContentInset.y` so you know what the offset is on the device at that moment. [doc](https://developer.apple.com/documentation/uikit/uiscrollview/2902259-adjustedcontentinset?language=objc) – hotdogsoup.nl Oct 10 '20 at 07:36
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);
    }

}
kalpesh satasiya
  • 799
  • 8
  • 18