Skip to content

Instantly share code, notes, and snippets.

@bugrym
Created March 25, 2020 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugrym/5679ea686d2dae88156a343f94c6fef8 to your computer and use it in GitHub Desktop.
Save bugrym/5679ea686d2dae88156a343f94c6fef8 to your computer and use it in GitHub Desktop.
Adaptive ScrollView
//
// AdaptiveScroll.swift
// RatingApp
//
// Created by Vladyslav Bugrym on 25.03.2020.
// Copyright © 2020 admin. All rights reserved.
//
import UIKit
class AdaptiveScrollView: UIScrollView {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc private func keyboardDidShow(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardSize = frame.cgRectValue.size
let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
adjustContentInsets(contentInsets)
}
@objc private func keyboardWillHide(_ notification: Notification) {
adjustContentInsets(.zero)
}
private func adjustContentInsets(_ contentInsets: UIEdgeInsets) {
contentInset = contentInsets
scrollIndicatorInsets = contentInsets
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment