-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIScrollView+Keyboard.swift
41 lines (36 loc) · 1.76 KB
/
UIScrollView+Keyboard.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// UIScrollView+Keyboard.swift
// Late Lateef
//
// Created by subhajit halder on 18/01/17.
// Copyright © 2017 Tanmoy. All rights reserved.
//
import Foundation
import UIKit
extension UIScrollView {
func scrollContentForViewFrame(activeFrame: CGRect, andParentView parentView: UIView) {
weak var showObserver: AnyObject?
weak var hideObserver: AnyObject?
showObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidShow, object: nil, queue: OperationQueue.main, using: {(note: Notification) -> Void in
let kbSize = (note.userInfo![UIKeyboardFrameEndUserInfoKey] as! CGRect).size
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
self.contentInset = contentInsets
self.scrollIndicatorInsets = contentInsets
var aRect = parentView.frame
aRect.size.height -= kbSize.height
let aOrigin = CGPoint(x: activeFrame.origin.x, y: activeFrame.origin.y + activeFrame.size.height)
if !aRect.contains(aOrigin) {
self.scrollRectToVisible(activeFrame, animated: true)
}
else {
self.scrollRectToVisible(CGRect.zero, animated: true)
}
NotificationCenter.default.removeObserver(showObserver as Any)
})
hideObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidHide, object: nil, queue: OperationQueue.main, using: {(note: Notification) in
let contentInsets = UIEdgeInsets.zero
self.contentInset = contentInsets
self.scrollIndicatorInsets = contentInsets
NotificationCenter.default.removeObserver(hideObserver as Any)
})
}}