我正在尝试为iPhone建立输入屏幕。屏幕上有许多输入字段。它们大多数位于屏幕顶部,但两个字段位于底部。当用户尝试在屏幕底部编辑文本时,键盘将弹出并覆盖屏幕。我找到了一种在发生这种情况时将屏幕向上移动的简单解决方案,但是结果是屏幕
始终 向上移动,并且当用户尝试编辑屏幕时,屏幕顶部的字段超出了范围。
有没有一种方法 只有 在编辑底部字段 时才 移动屏幕?
我使用了在这里找到的这段代码:
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)
}
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y -= 150
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 150
}
Apple在本文档中已对您的问题进行了很好的说明。此页面上的示例代码(at
Listing 4-1
)完全满足您的需求,仅当当前编辑应在键盘下时,它才会滚动视图。您只需要将所需的控件放在scrollViiew中即可。唯一的问题是,这是Objective-C,我想您需要在Swift..so
..这里:
声明一个变量
var activeField: UITextField?
然后添加这些方法
func registerForKeyboardNotifications()
{
//Adding notifies on keyboard appearing
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
}
func deregisterFromKeyboardNotifications()
{
//Removing notifies on keyboard appearing
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification)
{
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.scrollEnabled = true
var info : NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
var contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeFieldPresent = activeField
{
if (!CGRectContainsPoint(aRect, activeField!.frame.origin))
{
self.scrollView.scrollRectToVisible(activeField!.frame, animated: true)
}
}
}
func keyboardWillBeHidden(notification: NSNotification)
{
//Once keyboard disappears, restore original positions
var info : NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
var contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.scrollEnabled = false
}
func textFieldDidBeginEditing(textField: UITextField!)
{
activeField = textField
}
func textFieldDidEndEditing(textField: UITextField!)
{
activeField = nil
}
确保将ViewController声明为,UITextFieldDelegate
并在初始化方法中设置正确的委托:例如:
self.you_text_field.delegate = self
并记住要调用registerForKeyboardNotifications
viewInit和deregisterFromKeyboardNotifications
退出。
func registerForKeyboardNotifications(){
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}
func deregisterFromKeyboardNotifications(){
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWasShown(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.isScrollEnabled = true
var info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.height, right: 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = self.activeField {
if (!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}
@objc func keyboardWillBeHidden(notification: NSNotification){
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: -keyboardSize!.height, right: 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.isScrollEnabled = false
}
func textFieldDidBeginEditing(_ textField: UITextField){
activeField = textField
}
func textFieldDidEndEditing(_ textField: UITextField){
activeField = nil
}
我正在尝试为iPhone构建一个输入屏幕。屏幕有一个进审量字段。它们大多在屏幕顶部,但底部有两个字段。当用户尝试编辑屏幕底部的文本时,键盘会弹出,它会覆盖屏幕。当这种情况发生时,我找到了一个简单的解决方案来向上移动屏幕,但结果是屏幕总是向上移动,当用户尝试编辑那些时,屏幕顶部的字段移动到够不着的地方。 是否有办法使屏幕仅在编辑底部字段时移动? 我使用了我在这里找到的代码:
我已经搜索过了 此处:仅当键盘覆盖输入字段时,才向上移动视图 此处:键盘快速显示时移动文本字段 这里:当键盘存在时,如何使UITextField向上移动? 这里:https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManage
我尝试过许多不同的解决方案,但都不是我想要的。我想让键盘显示在内容上方(保持内容大小不变),同时能够滚动到键盘覆盖的输入元素。 我尝试过的每一个解决方案都会给我一个或另一个,但不是两个都给。 我尝试过的解决方案: 这里是解决方案。将android:WindowsOfInputMode=“adjustPan”和android:configChanges=“orientation | keyboard
Edittext字段被当前键盘覆盖。 人们看不到他们正在输入的内容。页面是一个片段。 下面是上述Edittext的XML布局: 它不起作用。也许我做得不对。你能帮我解决这个问题吗?https://i.stack.imgur.com/XmuXm.png
问题内容: 我正在使用Swift在iOS上进行编程,并且正在使用此代码移动,但是它不起作用。我正确调用了该函数,但是文本字段没有移动。我正在使用自动版式。 问题答案: 现有答案有一些改进。 首先, UIKeyboardWillChangeFrameNotification 可能是最好的通知,因为它处理的不仅是显示/隐藏,还包括由于键盘更改(语言,使用第三方键盘等)和旋转引起的更改(但下面的注释说明
我的视图中有六个UITextFields,我想决定视图是否必须移动。在移动视图之前,如何检查选择了哪个文本字段? 这是我显示键盘和移动视图的代码: