相关疑难解决方法(0)

当键盘出现时 - 如何开始编辑时,如何让UITextField向上移动?

使用iOS SDK:

我有一个UIView带有UITextField键盘的s.我需要它能够:

  1. UIScrollView一旦键盘出现,允许滚动内容以查看其他文本字段

  2. 自动"跳跃"(通过向上滚动)或缩短

我知道我需要一个UIScrollView.我已经尝试将我的类更改UIView为a UIScrollView但我仍然无法向上或向下滚动文本框.

我需要a UIView和a UIScrollView吗?一个进去另一个吗?

需要实现什么才能自动滚动到活动文本字段?

理想情况下,尽可能多的组件设置将在Interface Builder中完成.我只想编写需要它的代码.

注意:我正在使用的UIView(或UIScrollView)是由tabbar(UITabBar)启动的,它需要正常运行.


编辑:我正在添加滚动条,仅用于键盘出现时.即使它不需要,我觉得它提供了更好的界面,因为用户可以滚动和更改文本框.

我已经让它工作,我改变UIScrollView了键盘上下的框架大小.我只是使用:

-(void)textFieldDidBeginEditing:(UITextField *)textField { 
    //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
                     scrollView.frame.origin.y, 
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50);   //resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
   //keyboard will hide
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
       scrollView.frame.origin.y, 
     scrollView.frame.size.width,
      scrollView.frame.size.height + 215 - 50); //resize
}
Run Code Online (Sandbox Code Playgroud)

但是,这不会自动"向上移动"或将可见区域中的下部文本字段居中,这是我真正想要的.

objective-c uiscrollview uitextfield uikeyboard ios

1674
推荐指数
39
解决办法
61万
查看次数

当键盘覆盖 UITextView 时如何移动它

在此输入图像描述

UITextView当需要一些输入时,我需要将键盘移开。我正在使用以下代码,它与 a 完美配合,UITextField但与 a 则根本不起作用UITextView

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(DailyNotesViewController.keyboardWillShow(_:)),
                                                     name: UIKeyboardWillShowNotification,
                                                     object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(DailyNotesViewController.keyboardWillHide(_:)),
                                                     name: UIKeyboardWillHideNotification,
                                                     object: nil)
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
    let userInfo = notification.userInfo ?? [:]
    let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : -1)
    scrollView.contentInset.bottom += adjustmentHeight
    scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}

func keyboardWillShow(notification: NSNotification) {
    adjustInsetForKeyboardShow(true, notification: notification)
}

func …
Run Code Online (Sandbox Code Playgroud)

uitextview ios swift

5
推荐指数
1
解决办法
1914
查看次数