Why do you still use a deinit to get rid of notifications? That’s all automatic now. Not sure where to put this, but here’s a little extension I love for notificationcenter that makes it much nicer to work with.
extension Notification.Name {
static let DataDownloadComplete = Notification.Name("com.gargoylesoft.DidIWin:Data Downloaded Complete")
func post(object: Any? = nil, userInfo: [AnyHashable : Any]? = nil) {
NotificationCenter.default.post(name: self, object: object, userInfo: userInfo)
}
@discardableResult
func onPost(object: Any? = nil, queue: OperationQueue? = nil, using: @escaping (Notification) -> Void) -> NSObjectProtocol {
return NotificationCenter.default.addObserver(forName: self, object: object, queue: queue, using: using)
}
}
Now you can just say something like:
Notification.Name.DataDownloadComplete.post()
Hi Bryan,
A more simple aproach is:
override func viewDidLoad() {
...
NotificationCenter.default.addObserver(self, selector: #selector(adjustInsetsForKeyboard(notification:)), name: .UIKeyboardWillChangeFrame, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc func keyboardDidChangeFrame(notification: NSNotification) {
adjustInsetsForKeyboard(notification: notification)
}
@objc func adjustInsetsForKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo ?? [:]
let endKeyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let adjustment = abs(UIScreen.main.bounds.height - endKeyboardFrame.origin.y)
fgScrollView.contentInset.bottom = adjustment
fgScrollView.scrollIndicatorInsets.bottom = adjustment
}
you guys are far ahead of me…thank you for posting your stuff for me to learn. I really thank you guys!
1 Like
When I try it on my own, the auto scroll is not working. Not sure what I’m doing wrong.
Are you subscribing to the notification? It will help me if you paste your code here.
@bdmoakley, I think I figured it out. For my particular use case, the text field is not on the bottom of the page which means no scrolling is necessary to keep it visible. Manually adjusting the scroll offset fixed it though.