Created
March 25, 2020 12:10
-
-
Save bugrym/5679ea686d2dae88156a343f94c6fef8 to your computer and use it in GitHub Desktop.
Adaptive ScrollView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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