我正在尝试为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
}
我发现最适合我的是:
func textFieldDidBeginEditing(textField: UITextField) {
if textField == email || textField == password {
animateViewMoving(true, moveValue: 100)
}
}
func textFieldDidEndEditing(textField: UITextField) {
if textField == email || textField == password {
animateViewMoving(false, moveValue: 100)
}
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
let movementDuration:NSTimeInterval = 0.3
let movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations("animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
您还可以更改高度值。如果要将“if语句”用于所有文本字段,请删除它。
您甚至可以将其用于所有需要用户输入的控件,如TextView。
这是我的2美分:
您是否尝试过:https://github.com/hackiftekhar/IQKeyboardManager
安装Swift或Objective-C非常容易。
其工作原理如下:
IQKeyboardManager(Swift):-IQKeyboardManagerSwift通过CocoaPods提供,要安装它,只需在Podfile中添加以下行:(#236)
pod 'IQKeyboardManagerSwift'
在AppDelegate中。swift,只需导入IQKeyboardManagerSwift框架并启用IQKeyboardManager。
import IQKeyboardManagerSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
IQKeyboardManager.sharedManager().enable = true
// For Swift 4, use this instead
// IQKeyboardManager.shared.enable = true
return true
}
}
仅此而已。放松。
苹果公司在本文档中对您的问题进行了很好的解释。此页面上的示例代码(位于清单4-1)完全满足您的需要,只有当当前编辑应该在键盘下时,它才会滚动视图。您只需将所需的控件放在Scrollview中。唯一的问题是,这是Objective-C,我认为您需要在Swift中使用它。。所以这是:
声明变量
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声明为UITextFieldServer ate
并在初始化方法中设置正确的委托:例如:
self.you_text_field.delegate = self
记住在viewInit上调用registerworkeyboardnotifications,在退出时调用从键盘通知中注销。
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
我正在使用Swift进行iOS编程,我正在使用此代码移动,但它不起作用。我正确地调用函数,但文本字段不移动。我正在使用自动布局。
我尝试过许多不同的解决方案,但都不是我想要的。我想让键盘显示在内容上方(保持内容大小不变),同时能够滚动到键盘覆盖的输入元素。 我尝试过的每一个解决方案都会给我一个或另一个,但不是两个都给。 我尝试过的解决方案: 这里是解决方案。将android:WindowsOfInputMode=“adjustPan”和android:configChanges=“orientation | keyboard
我尝试过在键盘出现时向上移动屏幕。我成功地做到了,但我需要知道如何仅在单击底部时滚动视图,而不是顶部,因为键盘只是触摸下面的。 应用程序是这样的: 我们的观点
问题内容: 使用适用于Android和iOS的Phonegap 3.6.3创建移动应用程序。问题仅适用于Android,因为iOS的运行方式符合我的期望。 当我单击输入文本字段或文本区域时,将出现软键盘。它有时涵盖这些元素。 这些页面被放置在iScroll的底部,并位于另一个绝对放置的div中,因此,一旦屏幕弹出,我将无法滚动到其中任何一个页面。我怀疑键盘抬起时必须将Webview更改为较小。但是