gpt4 book ai didi

Swift 让键盘适应设备屏幕

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


所以我想快速学习,我正在制作这个简单的聊天应用程序......但我在控制键盘时遇到了一些问题。

  • 当键盘出现时我想向上推框架。如果我不这样做,我将无法访问我的文本字段。我发现这两个函数可以做到这一点...问题是设备屏幕不一样,如果我在另一台设备上运行该应用程序,我必须更改大小...
    例如,如果我使用 iPhon6,我将使用 220,如果我使用 5,则需要 150 ...如果我没记错的话。尺寸并不重要,重要的是不一样......

我认为这一定是一种轻松控制 future 的方法...我将不胜感激任何形式的帮助...

override func viewDidLoad() {

super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
//hide keyboard and get down the view
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 200
}
//show keyboard and get up the view
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y -= 200
}

//hide keyboard if you tap the screen
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}

谢谢!

最佳答案

Add a ScrollView Try Below Code.

 @IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var onTopScrollView: UIView!
var activeTextField: UITextField!
var keyboardHeight: CGFloat = 0

override func viewDidLoad() {

super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
//hide keyboard and get down the view
func keyboardWillHide(sender: NSNotification) {
keyboardHeight = 0
self.activeTextField = nil
let contentInsets = UIEdgeInsetsZero
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
scrollView.contentOffset = CGPointMake(0, 0)
}
//show keyboard and get up the view
func keyboardWillShow(sender: NSNotification) {
let info : NSDictionary = sender.userInfo!
let kbSize = (info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue() as CGRect!).size
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
keyboardHeight = kbSize.height
println(keyboardHeight)

scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;

var aRect = self.onTopScrollView.frame
aRect.size.height -= keyboardHeight;
if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) ) {
var rect = onTopScrollView.convertRect(CGRectZero, fromView: self.activeTextField)
rect.size = self.activeTextField.frame.size
//print(CGRectContainsPoint(aRect, self.activeTextField.frame.origin))
self.scrollView.scrollRectToVisible(rect, animated: true)
}
}

override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
super.viewWillDisappear(animated)
}

// handle TextField Delegates
func textFieldShouldReturn(textField: UITextField) -> Bool {

textField.resignFirstResponder()
return true
}

func textFieldDidBeginEditing(textField: UITextField) {
self.activeTextField = textField
}

关于Swift 让键盘适应设备屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372633/

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