📁
til
  • Today I Learned
  • Bookmarks
    • Bookmarks
  • Centos
    • How to limit bandwidth on CentOS
  • CI/CD
    • Add a mac as a GitLab runner
    • Setup CI pipeline for iOS projects on gitlab.com
  • cocoapods
    • private-spec-repo
    • CocoaPods save project.pbxproj file in XML plist format on Xcode 7.3.1
  • git
    • Git keeps asking password after El Capitan Upgrade
    • change-case-sensitivity-of-filename
    • interactive-rebase
    • Fork a repo
  • hacking
    • Decompile Android apk
  • homebrew
    • Bash Completion
  • ios
    • ats
    • Failed to open Xcode (LSOpenURLsWithRole() failed with error -10699)
    • Redirect to Settings Page
    • Retrieve expiry date of Provisioning Profile Certificate from .ipa
    • WKWebView set custom HTTP headers
    • IAP applicationUsername is nil
  • Jenkins
    • Create stage dynamically in declarative pipeline
  • mac
    • Catalina failed to sync iPhone with Finder
  • networking
    • shadowsocks vs. VPN
  • Objective-C
    • Keep subview in Scroll View always on screen
    • Custom View using xib
    • Scroll up TextField when keyboard shows
    • autolayout-hugging-vs-resistance
  • Regex
    • regex-chinese-char
  • SQL
    • update-json-value-in-postgresql
    • select-random-row-in-sql
  • SSH
    • verify-ssh-passphrase
  • SVN
    • Svn Checkout Directories only
  • swift
    • equatable
  • unix
    • Create and Grant Sudo Privileges to user
    • Create and Change Current Directory
    • Show ASCII Text welcome message when login with SSH
  • vim
    • Vim with Multiple Files
Powered by GitBook
On this page

Was this helpful?

  1. Objective-C

Keep subview in Scroll View always on screen

Previousshadowsocks vs. VPNNextCustom View using xib

Last updated 5 years ago

Was this helpful?

Demo:

  • 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
keep-subview-in-scrollView-always-on-screen-image