gpt4 book ai didi

ios - 调用文本字段委托(delegate)之前需要的键盘高度

转载 作者:行者123 更新时间:2023-11-30 13:38:21 25 4
gpt4 key购买 nike

所以我有以下代码:

 //MARK: - Textfield delegate Methods

func textFieldDidBeginEditing(textField: UITextField)
{

// update view based on updates in the constraints
self.view.layoutIfNeeded()
//perform animation to grow the dockview
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.dockViewHeightConstraint.constant = self.keyBoardHeight + 60
self.view.layoutIfNeeded()
}, completion: nil)
}

override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
}


func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
keyBoardHeight = contentInsets.bottom
}
}

我正在尝试做的事情如下。当用户单击文本字段时,键盘应该打开,我制作的 View 应该向上移动。我通过 NSNotification 触发的 keyboardWillShow 函数获取键盘的高度。

问题如下:当我运行代码时,在NSNotification之前调用textFieldDidBeginEditing,使我的self.keyBoardHeight值为0(初始值)。有没有办法在按下文本字段之前调用 NSNotification 部分?

最佳答案

在 viewController 中设置 isKeyboardShown 标志来跟踪键盘是否显示,与元素“myElement”相交的高度可以在 fieldMovement 属性中捕获,如下所示

    if !isKeyboardShown
{
if let info = notification.userInfo
{
if fieldMovement == nil
{
let window = UIApplication.sharedApplication().keyWindow
var keyboardEndRect = CGRectZero

//Get frame of keyboard view object
((info[UIKeyboardFrameEndUserInfoKey] as! NSValue)).getValue(&keyboardEndRect)

/* Convert the frame from the window's coordinate system to our view's coordinate system */
keyboardEndRect = view.convertRect(keyboardEndRect, fromView: window)

/* Find out how much of our view is being covered by the keyboard */
fieldMovement = CGRectIntersection(myElement.frame, keyboardEndRect).height
}

//Get animation duration of keyboard
var animationDuration: NSTimeInterval = 0
(info[UIKeyboardAnimationDurationUserInfoKey] as! NSValue).getValue(&animationDuration)

// Move views up by intersected height
UIView.animateWithDuration(animationDuration)
{
//animate your objects here
}
}
isKeyboardShown = true
}

关于ios - 调用文本字段委托(delegate)之前需要的键盘高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35844682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com