UITextfield显示在键盘 – 初学者的顶部

我有textboxes 。 当我点击它,键盘popup。 但是,由于uitextfield位于uitextfield下方,所以在popup键盘时会覆盖它。 (键盘显示在文本字段上方,因此用户无法看到它)。

我需要一种方法来移动textfield ,以便用户可以看到它。 iPhone的facebook应用程序有一个解决方法,他们收缩UIComponents视图上显示的textfield ,所以它不包括从键盘。

有人能指点我一个教程或示例代码开始?

选项-1:你可以在这个链接中参考这个被接受的代码作为答案:

当键盘存在时如何使UITextField向上移动?

这一定会帮助你,并给你你需要的解决scheme。

选项-2:这里也是一个现成的教程:

http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/

我认为你肯定会得到这个帮助

选项-3:你可以参考这个:

http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/

选项-4:苹果文档:

https://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

希望这可以帮助你。

工作演示

在iOS9上处理空间案例,如预测文本打开和closures以及不同大小的键盘。

  1. 全局variables来存储您的默认视图框架

 var defaultFrame: CGRect! 

  1. 在init()保存您的初始框架和订阅键盘通知

 defaultFrame = self.frame NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil) 

  1. 简单的function来重新定位你的看法

 func moveViewWithKeyboard(height: CGFloat) { self.frame = CGRectOffset(defaultFrame, 0, height) } 

  1. 根据内部的密码input偏移进行移动

 func keyBoardWillShow(notification: NSNotification) { let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() moveViewWithKeyboard(-frame.height) } func keyBoardWillHide(notification: NSNotification) { moveViewWithKeyboard(0) } 

最好的方法是使用UIScrollView并在需要开始编辑文本时向上移动。 这个问题/答案会给你一个粗略的指导,让它成立

当键盘出现时,如何使UITextField向上移动

它可能会帮助你… bottomLayoutConstrain是从文本字段到底部视图的底部约束,从你想要的位置开始。 SWIFT代码:

  NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHideNotification:", name: UIKeyboardWillHideNotification, object: nil) func keyboardWillShowNotification(notification:NSNotification){ let keyboardInfo:NSDictionary = notification.userInfo! let keyboardFrameBegin:AnyObject = keyboardInfo.valueForKey(UIKeyboardFrameEndUserInfoKey)! keyboardFrameBeginRect = keyboardFrameBegin.CGRectValue self.view.layoutIfNeeded() self.bottomLayoutConstrain.constant = self.keyboardFrameBeginRect!.height self.view.layoutIfNeeded() } func keyboardWillHideNotification(notification:NSNotification){ self.view.layoutIfNeeded() bottomLayoutConstrain?.constant = 0 self.view.layoutIfNeeded() } deinit { // perform the deinitialization NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) // NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil) NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) } 

目标C代码

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(void) keyboardWillShowNotification:(NSNotification *) notification{ NSDictionary *keyboardInfo = notification.userInfo; keyboardFrameBeginRect = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; [self.view layoutIfNeeded]; // [UIView animateWithDuration:1.0 animations:^{ topLayoutConstraints.constant = -keyboardFrameBeginRect.size.height; bottomLayoutConstrain.constant = keyboardFrameBeginRect.size.height; // }]; [self.view layoutIfNeeded]; } -(void) keyboardWillHideNotification:(NSNotification *) notification{ [self.view layoutIfNeeded]; topLayoutConstraints.constant = 0; bottomLayoutConstrain.constant = 0; [self.view layoutIfNeeded]; }