> For the complete documentation index, see [llms.txt](https://ryanzzff.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ryanzzff.gitbook.io/til/objective-c/keep-subview-in-scrollview-always-on-screen.md).

# Keep subview in Scroll View always on screen

## Demo:

![keep-subview-in-scrollView-always-on-screen-image](https://911427103-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Luw7uParZv_6tuDimIv%2F-Luw9PUbo17oEMG63GU-%2F-Luw9QkokYO2QVs_jyZQ%2Fkeep-subview-in-scrollView-always-on-screen.gif?generation=1575114886735717\&alt=media)

* contentOffset is 0 if the scrollView is scrolled top
* contentOffset of a scrollView will increase as user scroll down

To keep the overlayView on the same position on screen, just increase the posY of the frame of the overlayView as the contentOffset increase

```
@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *overlayView;

@property (assign, nonatomic) float initialOverlayY;

@end
```

```
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.scrollView addSubview:self.overlayView];
    self.initialOverlayY = self.overlayView.frame.origin.y;
    self.scrollView.delegate = self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect frame = self.overlayView.frame;
    float minHeight = 40; // configurable
    if (scrollView.contentOffset.y + minHeight <= self.initialOverlayY) {
        // keep the posY, so the overlayView move with the scrollView
        frame.origin.y = self.initialOverlayY;
    } else {
        // keep the overlayView on the same position of screen
        frame.origin.y = scrollView.contentOffset.y + minHeight;
    }
    self.overlayView.frame = frame;
}
@end
```
