【问题标题】:UITextfield to be displayed on top of Keyboard - Beginner要显示在键盘顶部的 UITextfield - 初学者
【发布时间】:2012-03-16 06:20:24
【问题描述】:

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

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

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

【问题讨论】:

标签: iphone objective-c ios uiscrollview uitextfield


【解决方案1】:

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

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

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

OPTION-2:这里还有一个现成的教程:

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

我想你肯定会得到这个帮助

OPTION-3:你可以参考这个:

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

OPTION-4: Apple 文档:

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

希望对你有所帮助。

【讨论】:

  • 它说UIKeyboardBoundsUserInfoKey 已被弃用。你知道有什么替代方法吗?
  • @shajem:您可以参考此链接以获取弃用UIKeyboardsUserInfoKey stackoverflow.com/questions/2807339/… 的解决方案
  • 虽然理论上可以回答这个问题,但it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
  • @BilltheLizard:如果我复制他们的答案并将其放在这里,然后提供链接作为参考,那么在这种情况下,我认为应该得到正确答案的人不会得到他的应得的信用。如果我错了,请告诉我。没什么私人的:)谢谢
  • 请在此处总结答案并附上链接以供参考。如果链接失效,那么您当前的答案将变得毫无价值。这就是为什么我们要求人们在此处完全回答问题。
【解决方案2】:

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

  1. 用于存储默认视图框架的全局变量

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. 重定位视图的简单函数

func moveViewWithKeyboard(height: CGFloat) {
    self.frame = CGRectOffset(defaultFrame, 0, height)
}
  1. 根据内部键盘偏移量移动

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

【讨论】:

  • 在 ios 11 上不起作用。只要我按下键盘上的一个键,输入字段就会消失
【解决方案3】:

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

How to make a UITextField move up when keyboard is present

【讨论】:

    【解决方案4】:

    它可以帮助你... 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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-08
      • 2015-10-06
      • 1970-01-01
      • 2020-03-02
      • 2015-07-15
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      相关资源
      最近更新 更多