-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from martinpucik/additionalInputViewBottomCon…
…straintConstant Added `additionalInputViewBottomConstraintConstant` to KeyboardManager
- Loading branch information
Showing
10 changed files
with
186 additions
and
102 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
71 changes: 71 additions & 0 deletions
71
Example/Sources/Example ViewControllers/AdditionalBottomSpaceExampleViewController.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// AdditionalBottomSpaceExampleViewController.swift | ||
// Example | ||
// | ||
// Created by Martin Púčik on 16.05.2022. | ||
// Copyright © 2022 Nathan Tannar. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import InputBarAccessoryView | ||
|
||
final class AdditionalBottomSpaceExampleViewController: CommonTableViewController { | ||
|
||
private lazy var keyboardManager = KeyboardManager() | ||
|
||
private lazy var additionalBottomBar: UIView = { | ||
let view = UIView() | ||
view.backgroundColor = .systemBlue | ||
view.translatesAutoresizingMaskIntoConstraints = false | ||
return view | ||
}() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
view.addSubview(inputBar) | ||
view.addSubview(additionalBottomBar) | ||
|
||
NSLayoutConstraint.activate([ | ||
additionalBottomBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), | ||
additionalBottomBar.leadingAnchor.constraint(equalTo: view.leadingAnchor), | ||
additionalBottomBar.trailingAnchor.constraint(equalTo: view.trailingAnchor), | ||
additionalBottomBar.heightAnchor.constraint(equalToConstant: 50) | ||
]) | ||
|
||
view.layoutIfNeeded() | ||
|
||
keyboardManager.additionalInputViewBottomConstraintConstant = { | ||
var safeBottomInset: CGFloat = self.view.safeAreaInsets.bottom | ||
if let windowBottomInset = UIApplication.shared.windows.first?.safeAreaInsets.bottom, | ||
safeBottomInset != windowBottomInset { | ||
safeBottomInset = windowBottomInset | ||
} | ||
return -(self.additionalBottomBar.frame.height + safeBottomInset) | ||
} | ||
|
||
// Binding the inputBar will set the needed callback actions to position the inputBar on top of the keyboard | ||
keyboardManager.bind(inputAccessoryView: inputBar) | ||
|
||
// Binding to the tableView will enabled interactive dismissal | ||
keyboardManager.bind(to: tableView) | ||
} | ||
|
||
override func viewWillDisappear(_ animated: Bool) { | ||
super.viewWillDisappear(animated) | ||
|
||
/// This replicates instagram's behavior when commenting in a post. As of 2020-09, it appears like they have one of the best product experiences of this handling the keyboard when dismissing the UIViewController | ||
self.inputBar.inputTextView.resignFirstResponder() | ||
/// This is set because otherwise, if only partially dragging the left edge of the screen, and then cancelling the dismissal, on viewDidAppear UIKit appears to set the first responder back to the inputTextView (https://stackoverflow.com/a/41847448) | ||
self.inputBar.inputTextView.canBecomeFirstResponder = false | ||
} | ||
|
||
override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
|
||
/// The opposite of `viewWillDisappear(_:)` | ||
self.inputBar.inputTextView.canBecomeFirstResponder = true | ||
self.inputBar.inputTextView.becomeFirstResponder() | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
File renamed without changes.
This file contains 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
Oops, something went wrong.