다양한 iOS 기기의 키보드 높이는 다릅니다. 프로그래밍 방식으로 장치의 키보드 높이를 얻는 방법을 아는 사람이 있습니까?
답변
Swift에서 :
UIKeyboardWillShowNotification
알림 을 구독하면 키보드 높이를 알 수 있습니다 . (표시되기 전에 높이가 무엇인지 알고 싶다고 가정합니다).
이 같은:
스위프트 2
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
스위프트 3
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil
)
스위프트 4
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
그런 다음 다음 keyboardWillShow
과 같은 기능 에서 높이에 액세스 할 수 있습니다 .
스위프트 2
func keyboardWillShow(notification: NSNotification) {
let userInfo: NSDictionary = notification.userInfo!
let keyboardFrame: NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let keyboardHeight = keyboardRectangle.height
}
스위프트 3
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
스위프트 4
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
답변
Swift 3.0 및 Swift 4.1
1- viewWillAppear
방법에 알림 등록 :
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
2- 호출 할 방법 :
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
print(keyboardHeight)
}
}
답변
Swift 4 및 제약
tableview에 하단 안전 영역을 기준으로 하단 제약 조건을 추가하십시오. 제 경우에는 제약 조건을 tableViewBottomLayoutConstraint라고합니다.
@IBOutlet weak var tableViewBottomLayoutConstraint: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(notification:)), name: .UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow , object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide , object: nil)
}
@objc
func keyboardWillAppear(notification: NSNotification?) {
guard let keyboardFrame = notification?.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
return
}
let keyboardHeight: CGFloat
if #available(iOS 11.0, *) {
keyboardHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
} else {
keyboardHeight = keyboardFrame.cgRectValue.height
}
tableViewBottomLayoutConstraint.constant = keyboardHeight
}
@objc
func keyboardWillDisappear(notification: NSNotification?) {
tableViewBottomLayoutConstraint.constant = 0.0
}
답변
Swift 4.2 업데이트
private func setUpObserver() {
NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: UIResponder.keyboardWillShowNotification, object: nil)
}
선택기 방법 :
@objc fileprivate func keyboardWillShow(notification:NSNotification) {
if let keyboardRectValue = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardRectValue.height
}
}
신장:
private extension Selector {
static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:))
}
Swift 3.0 업데이트
private func setUpObserver() {
NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: .UIKeyboardWillShow, object: nil)
}
선택기 방법 :
@objc fileprivate func keyboardWillShow(notification:NSNotification) {
if let keyboardRectValue = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardRectValue.height
}
}
신장:
private extension Selector {
static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:))
}
팁
UIKeyboardDidShowNotification 또는 UIKeyboardWillShowNotification이 두 번 호출되고 다른 결과를 얻을 수 있습니다.이 문서에서는 두 번 호출 된 이유를 설명했습니다.
Swift 2.2에서
Swift 2.2는 선택자에 대한 문자열 사용을 중단하고 대신 새로운 구문 : #selector
.
다음과 같은 것 :
private func setUpObserver() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: .keyboardWillShow, name: UIKeyboardWillShowNotification, object: nil)
}
선택기 방법 :
@objc private func keyboardWillShow(notification:NSNotification) {
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let keyboardHeight = keyboardRectangle.height
editorBottomCT.constant = keyboardHeight
}
신장:
private extension Selector {
static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(_:))
}
답변
여기에 더 짧은 버전 :
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
}
}
답변
스위프트 4 .
가장 간단한 방법
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight : Int = Int(keyboardSize.height)
print("keyboardHeight",keyboardHeight)
}
}
답변
스위프트 5
override func viewDidLoad() {
// Registering for keyboard notification.
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
/* UIKeyboardWillShowNotification. */
@objc internal func keyboardWillShow(_ notification : Notification?) -> Void {
var _kbSize:CGSize!
if let info = notification?.userInfo {
let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
// Getting UIKeyboardSize.
if let kbFrame = info[frameEndUserInfoKey] as? CGRect {
let screenSize = UIScreen.main.bounds
//Calculating actual keyboard displayed size, keyboard frame may be different when hardware keyboard is attached (Bug ID: #469) (Bug ID: #381)
let intersectRect = kbFrame.intersection(screenSize)
if intersectRect.isNull {
_kbSize = CGSize(width: screenSize.size.width, height: 0)
} else {
_kbSize = intersectRect.size
}
print("Your Keyboard Size \(_kbSize)")
}
}
}