UITextfield将显示在键盘 - 初学者之上

时间:2012-02-26 14:16:33

标签: iphone objective-c ios uiscrollview uitextfield

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

我需要一种方法来移动textfield,以便用户可以看到它。 iPhone facebook应用有一个解决方法,他们缩小视图上的UIComponents以在顶部显示textfield,因此它不会从键盘覆盖。

有人可以指点我开始使用教程或示例代码吗?

4 个答案:

答案 0 :(得分:5)

选项-1:您可以参考以下链接中已接受为答案的此代码:

How to make a UITextField move up when keyboard is present?

这肯定会帮助您并为您提供所需的解决方案。

选项-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: Apple文档:

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

希望这会对你有所帮助。

答案 1 :(得分:2)

working demo

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

  1. 存储默认视图框架的全局变量
  2. var defaultFrame: CGRect!

    1. 在init()中保存您的初始框架&订阅键盘通知
    2. defaultFrame = self.frame
      NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
      NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

      1. 重新定位视图的简单功能
      2. func moveViewWithKeyboard(height: CGFloat) {
            self.frame = CGRectOffset(defaultFrame, 0, height)
        }

        1. 基于内部键盘偏移移动
        2. func keyBoardWillShow(notification: NSNotification) {
              let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
              moveViewWithKeyboard(-frame.height)
          }
          func keyBoardWillHide(notification: NSNotification) {
              moveViewWithKeyboard(0)
          }

答案 2 :(得分:1)

最好的方法是使用UIScrollView并在需要开始编辑文本时将其移动。 这个问题/答案将为您提供设置的粗略指南

How to make a UITextField move up when keyboard is present

答案 3 :(得分:0)

  

它可能对你有帮助... bottomLayoutConstrain是来自底层的约束   文本字段从您想要的位置到视图的底部。斯威夫特代码:

        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];
}