【问题标题】:How to make scrollview up to keyboard height according to iPhone sizes in swift如何根据 iPhone 尺寸快速使滚动视图达到键盘高度
【发布时间】:2020-04-08 18:01:40
【问题描述】:

我添加了scrollView 上、左、右、下0。在scrollView 中我添加了高度为550 的viewinScrollview,在这个视图中我添加了fieldsContainerView。如下所示

storyboard view hierarchy img

如果我在 iPhone8 中运行项目,那么设计看起来不错,但如果我在 iPhone 11 pro 中运行,我会看到不必要的 scrollview。我给scrollview 颜色是绿色,viewinScrollview 颜色是白色。

在 iPhone 11 pro 中我得到如下所示,绿色空间是滚动视图,我不想要键盘后的那个空间 iPhone 11 pro

这是 iPhone8 模拟器 iPhone8 我在所有 iPhone 尺寸都需要这样。

代码如下:

 override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name: UIResponder.keyboardDidHideNotification, object: nil)

  }

  @objc func keyboardDidShow(notification: NSNotification) {
      let info = notification.userInfo
      let keyBoardSize = info![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
      scrolView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyBoardSize.height, right: 0.0)
      scrolView.scrollIndicatorInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyBoardSize.height, right: 0.0)
  }

  @objc func keyboardDidHide(notification: NSNotification) {
      scrolView.contentInset = UIEdgeInsets.zero
      scrolView.scrollIndicatorInsets = UIEdgeInsets.zero
  }

请帮我删除所有 iPhone 尺寸的绿色空间。

【问题讨论】:

  • 尝试从 keyBoardSize.height 中减去 view.safeAreaInsets.bottom。

标签: ios swift keyboard uitextfield scrollviewer


【解决方案1】:

这里是帮助您管理scrollView、TableView、CollectionView 中的Textfields 的库。你可以使用这个库。借助此库,您无需管理每个设备的滚动视图内容插图。

Github 链接:- https://github.com/hackiftekhar/IQKeyboardManager

【讨论】:

  • 我们不能只滚动所有 iphone 尺寸的键盘高度,而不添加库.. 请建议我
  • 那么你必须为所有的 iPhone 手动管理事情,这太复杂了。
【解决方案2】:

删除故事板中滚动视图的背景颜色。

您可以按照我的方法进行更多说明。

moving-keyboard-when-editing-multiple-textfields-with-constraints-swift

【讨论】:

  • 好的,只有我想要这样,但是在第三个文本字段中为什么它又向上移动了,没有必要向右移动 iphoneXR
  • 兄弟你没有回答我的评论??我在我的项目中添加了 pod 'TPKeyboardAvoiding' 但它不起作用?为什么?怎么了?,为什么你没有导入 TPKeyboardAvoiding
  • 嗨@iPhoneDev,您不需要导入 'TPKeyboardAvoiding' 。在您的故事板中,将滚动视图类名称设置为 UIViewcontroller 中的“TPKeyboardAvoidingScrollview”。它会起作用的。使用此链接了解更多详情stackoverflow.com/questions/1126726/…
  • 它有效,我已经接受了你的回答,但是在你的项目键盘中,你在最后一个文本字段中的所有文本字段都有下一个,你已经完成了.. 怎么样??我只得到返回按钮
  • 在每个文本字段的检查器选项卡中,将返回键保留为默认值。这就是我在项目中所做的。
【解决方案3】:

使用以下库来帮助管理任何 contentView 中的 textField 或 textView

Github 链接:- https://github.com/Sunilkumar01091985/KeyBoard/tree/master

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多