Skip to content

Instantly share code, notes, and snippets.

@simonbs
Created January 24, 2022 21:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbs/3afc91a2e492a934f51f3c232872d448 to your computer and use it in GitHub Desktop.
Save simonbs/3afc91a2e492a934f51f3c232872d448 to your computer and use it in GitHub Desktop.
Avoid undocked keyboard at bottom.
// Sample code made in response to this tweet:
// https://twitter.com/stroughtonsmith/status/1485719749673820163
final class MainView: UIView {
private let textView: UITextView = {
let this = UITextView()
this.translatesAutoresizingMaskIntoConstraints = false
this.text = "I'm a text view"
this.font = .preferredFont(forTextStyle: .body)
this.textColor = .label
return this
}()
private let leadingView: UIView = {
let this = UIView()
this.translatesAutoresizingMaskIntoConstraints = false
this.backgroundColor = .systemBlue
this.isUserInteractionEnabled = false
return this
}()
private let trailingView: UIView = {
let this = UIView()
this.translatesAutoresizingMaskIntoConstraints = false
this.backgroundColor = .systemBlue
this.isUserInteractionEnabled = false
return this
}()
init() {
super.init(frame: .zero)
setupView()
setupLayout()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
backgroundColor = .systemBackground
keyboardLayoutGuide.followsUndockedKeyboard = true
addSubview(textView)
addSubview(leadingView)
addSubview(trailingView)
}
private func setupLayout() {
let leadingViewBottomConstraint = leadingView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
let trailingViewBottomConstraint = trailingView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
leadingViewBottomConstraint.priority = .defaultHigh
trailingViewBottomConstraint.priority = .defaultHigh
NSLayoutConstraint.activate([
textView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: trailingAnchor),
textView.topAnchor.constraint(equalTo: topAnchor),
textView.bottomAnchor.constraint(equalTo: bottomAnchor),
leadingView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
leadingViewBottomConstraint,
leadingView.widthAnchor.constraint(equalToConstant: 300),
leadingView.heightAnchor.constraint(equalToConstant: 170),
trailingView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
trailingViewBottomConstraint,
trailingView.widthAnchor.constraint(equalToConstant: 300),
trailingView.heightAnchor.constraint(equalToConstant: 170)
])
keyboardLayoutGuide.setConstraints([
leadingView.bottomAnchor.constraint(equalTo: keyboardLayoutGuide.topAnchor, constant: -15)
], activeWhenNearEdge: [.bottom, .leading])
keyboardLayoutGuide.setConstraints([
trailingView.bottomAnchor.constraint(equalTo: keyboardLayoutGuide.topAnchor, constant: -15)
], activeWhenNearEdge: [.bottom, .trailing])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment