gpt4 book ai didi

ios - 如果只选择了某个 textView 如何移动 View

转载 作者:搜寻专家 更新时间:2023-10-31 08:32:36 24 4
gpt4 key购买 nike

您好,我正在使用此代码在选择 textView 时移动我的 View ,这是为了确保我的 texView 在键盘弹出时可见。

override func viewDidLoad() {
super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)
}


func keyboardWillHide(sender: NSNotification) {

let userInfo: [NSObject : AnyObject] = sender.userInfo!
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
self.view.frame.origin.y += keyboardSize.height

}

func keyboardWillShow(sender: NSNotification) {

let userInfo: [NSObject : AnyObject] = sender.userInfo!

let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size

if keyboardSize.height == offset.height {
if self.view.frame.origin.y == 0 {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.frame.origin.y -= keyboardSize.height
})
}
} else {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.frame.origin.y += keyboardSize.height - offset.height
})
}
print(self.view.frame.origin.y)

}


override func viewWillDisappear(animated: Bool) {

NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)

}

enter image description here

如果底部的 textView 被选中,我如何才能移动 View ?因为目前,如果您选择最上面的 textView,它会将其一半移出屏幕。

非常感谢任何帮助,提前致谢。

最佳答案

在通知回调中检查 bottomTextView.isFirstResponder() 并仅在其为真时才移动 View 。否则不要移动 View 。

关于ios - 如果只选择了某个 textView 如何移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699570/

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