Button that doesn't move while scrolling

How do i have a button on a view controller that doesn’t move while scrolling?

Take a look at the scrollViewDidScroll method in the UIScrollViewDelegate protocol. Once hooked up, that method will be called whenever your view scrolls.

From there, you can update the frame of your button using the scroll view’s content offset. Basically, you would be moving the button around in the overall scroll content view so that the button’s position on the screen stays the same.

Here is some Objective-C code from an old project.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
       CGRect frame = self.button.frame;
       frame.origin.x = scrollView.contentOffset.x + 80;
       frame.origin.y = scrollView.contentOffset.y + 110;
       [self.button setFrame:frame];

That code will keep the button positioned at 80,110 regardless of where you are located in the scroll view.

Have fun!

1 Like