ODScrollView 1.0.3

ODScrollView 1.0.3

Orçun Deniz 维护。



  • Orçun Deniz

ODScrollView

Platform Generic badge Build Status Version Generic badge Generic badge License

ODScrollView simply extends UIScrollView and automatically adjusts scrollable text areas like UITextField and UITextView vertically based on the visibility of the keyboard to offer a better user experience.

特性

  • 在键盘出现/消失时,自动向上/向下移动可响应首个视图( UIViews that adopt the UITextInput protocol),例如 UITextField, UITextView, UISearchTextField 或任何自定义的 adpots UITextInput 协议的 UIView。

    • 注意,如果 UITextInput 的 frame 不适合 ODScrollView 和键盘之间的剩余区域,则 ODScrollView 将根据光标位置调整 UITextInput 而不是 frame。在这种情况下,可以使用 "trackTextInputCursor" 功能。《示例》
  • 可以为每个 UITextInput( 分别向上和向下调整方向设置)分别应用调整边距。默认值为 20 CGFloat。

  • 可以为每个 UITextInput( 分别启用/禁用调整)。默认值为 true。

  • 调整方向 - .Top, .Center, .Bottom - 可以分别应用于每个 UITextInput。默认为 .Bottom。《示例》

  • 调整选项决定了 ODScrollView 如何调整。默认为 .Always。

    • .Always:无论 UITextInput 是否与显示的键盘重叠,ODScrollView 都会调整 ODScrollView 中任何位置的 UITextInput。《示例》
    • .IfNeeded:只有当 UITextInput 与显示的键盘重叠时,ODScrollView 才会调整。《示例》
  • 除了 UIScrollView.keyboardDismissModes,还可以通过点击 ODScrollViewDelegate 提供的 UIView 来收起键盘。在键盘收起后,ODScrollView 可以返回到其原始位置。默认为 nil 和 false。《示例》

示例

所有UITextInputs的adjustmentMargin = 20
所有UITextInputs的adjustmentEnabled = true
所有UITextInputs的adjustmentDirection = .Bottom
adjustmentOption = .Always
hideKeyboardByTappingToView = ViewController.view
isResettingAdjustmentEnabled = true
UIScrollView.keyboardDismissMode = .onDrag

another Examples

要求

  • iOS 12.0+
  • Xcode 10.2+
  • Swift 5+

安装

CocoaPods

请查看CocoaPods的“入门”标签。

要将ODScrollView添加到您的项目中,请将以下'Podfile'添加到您项目中

use_frameworks!
target 'MY_APP' do
  pod 'ODScrollView', '~> 1.0'
end

然后运行

pod install

Carthage

请查看Carthage文档以了解如何添加安装。ODScrollView框架已经设置了共享方案。

Carthage安装

您可以使用以下命令使用Homebrew安装Carthage

$ brew update
$ brew install carthage

要使用Carthage将ODScrollView集成到您的Xcode项目中,请在Cartfile中指定它

github "orcundeniz/ODScrollView" ~> 1.0 

Swift 包管理器

Swift 包管理器是一种用于自动分发 Swift 代码的工具,与 Swift 编译器集成。

一旦你设置好你的 Swift 包,添加 ODScrollView 作为依赖与将其添加到 Package.swift 的依赖值一样简单。

dependencies: [
    .Package(url: "https://github.com/orcundeniz/ODScrollView.git", majorVersion: 1)
]

或者,你可以在 XCode 中直接添加

 XCode -> File -> Swift Packages -> Add Package Dependency -> https://github.com/orcundeniz/ODScrollView.git

使用

1 - 首先,您需要正确设置 ODScrollView 和其内容视图。由于 ODScrollView 只是 UIScrollView,您可以使用与 UIScrollView 相同的方式实现 ODScrollView。您可以选择使用 storyboard 或编程方式创建 ODScrollView。

如果您是使用编程方式创建 ODScrollView,请从步骤 4 继续操作。

Storyboard 中创建 UIScrollView 的建议方式
- If you are using Content Layout Guide and Frame Layout Guide:
    1.1 - scrollView: Place UIScrollView anywhere you want to use.  
    1.2 - contentView: Place UIView inside scrollView.
    1.3 - Set contentView's top, bottom, leading and trailing constraints to Content Layout Guide's constraints.
    1.4 - Set contentView's width equal to Frame Layout Guide's width.
    1.5 - Set contentView's height equal to Frame Layout Guide's height or set static height which is larger than scrollView's height.
    1.6 - Build your UI inside contentView.
    
- If you are NOT using Content Layout Guide and Frame Layout Guide:
    1.1 - scrollView: Place UIScrollView anywhere you want to use.  
    1.2 - contentView: Place UIView inside scrollView.
    1.3 - Set contentView's top, bottom, leading and trailing constraints to 0.
    1.4 - Set contentView's width equal to scrollView's width.
    1.5 - Set contentView's height equal to scrollView's superview's height or set static height which is larger than scrollView's height.
    1.6 - Build your UI inside contentView.

2 - 在 Storyboard 的身份检查器中将 scrollView 的类从 UIScrollView 改为 ODScrollView。

3 - 在 ViewController 中为 scrollView 和 contentView 创建 IBOutlets。

4 - 在 ViewController 的 ViewDidLoad() 内调用以下方法

override func viewDidLoad() {
    super.viewDidLoad()

    //ODScrollView setup
    scrollView.registerContentView(contentView)
    scrollView.odScrollViewDelegate = self
}  

5 - 可选:您仍然可以使用 UIScrollView 的功能

override func viewDidLoad() {
    super.viewDidLoad()

    //ODScrollView setup
    scrollView.registerContentView(contentView)
    scrollView.odScrollViewDelegate = self

    // UIScrollView setup
    scrollView.delegate = self // UIScrollView Delegate
    scrollView.keyboardDismissMode = .onDrag // UIScrollView keyboardDismissMode. Default is .none.

    UITextView_inside_contentView.delegate = self
}

6 - 从 ViewController 中采用 ODScrollViewDelegate 并决定 ODScrollView 选项

extension ViewController: ODScrollViewDelegate {

    // MARK:- State Notifiers: are responsible for notifiying ViewController about what is going on while adjusting. You don't have to do anything if you don't need them.

    // #Optional
    // Notifies when the keyboard showed.
    func keyboardDidShow(by scrollView: ODScrollView) {}

    // #Optional
    // Notifies before the UIScrollView adjustment.
    func scrollAdjustmentWillBegin(by scrollView: ODScrollView) {}

    // #Optional
    // Notifies after the UIScrollView adjustment.
    func scrollAdjustmentDidEnd(by scrollView: ODScrollView) {}

    // #Optional
    // Notifies when the keyboard hid.
    func keyboardDidHide(by scrollView: ODScrollView) {}

    // MARK:- Adjustment Settings

    // #Optional
    // Specifies the margin between UITextInput and ODScrollView's top or bottom constraint depending on AdjustmentDirection
    func adjustmentMargin(for textInput: UITextInput, inside scrollView: ODScrollView) -> CGFloat {
        if let textField = textInput as? UITextField, textField == self.UITextField_inside_contentView {
            return 20
        } else {
            return 40
        }
    }

    // #Optional
    // Specifies that whether adjustment is enabled or not for each UITextInput seperately.
    func adjustmentEnabled(for textInput: UITextInput, inside scrollView: ODScrollView) -> Bool {
        if let textField = textInput as? UITextField, textField == self.UITextField_inside_contentView {
            return true
        } else {
            return false
        }
    }


    // Specifies adjustment direction for each UITextInput. It means that  some of UITextInputs inside ODScrollView can be adjusted to the bottom, while others can be adjusted to center or top.
    func adjustmentDirection(selected textInput: UITextInput, inside scrollView: ODScrollView) -> AdjustmentDirection {
        if let textField = textInput as? UITextField, textField == self.UITextField_inside_contentView {
            return .bottom
        } else {
            return .center
        }
    }

    /**
     - Always : ODScrollView always adjusts the UITextInput which is placed anywhere in the ODScrollView.
     - IfNeeded : ODScrollView only adjusts the UITextInput if it overlaps with the shown keyboard.
     */
    func adjustmentOption(for scrollView: ODScrollView) -> AdjustmentOption {
        .Always
    }

    // MARK: - Hiding Keyboard Settings

    /**
     #Optional
     
     Provides a view for tap gesture that hides keyboard.

     By default, keyboard can be dismissed by keyboardDismissMode of UIScrollView.

     keyboardDismissMode = .none
     keyboardDismissMode = .onDrag
     keyboardDismissMode = .interactive

     Beside above settings:

     - Returning UIView from this, lets you to hide the keyboard by tapping the UIView you provide, and also be able to use isResettingAdjustmentEnabled(for scrollView: ODScrollView) setting.

     - If you return nil instead of UIView object, It means that hiding the keyboard by tapping is disabled.
     */
    func hideKeyboardByTappingToView(for scrollView: ODScrollView) -> UIView? {
        self.view
    }

    /**
     #Optional
    
     Resets the scroll view offset - which is adjusted before - to beginning its position after keyboard hid by tapping to the provided UIView via hideKeyboardByTappingToView.

     ## IMPORTANT:
     This feature requires a UIView that is provided by hideKeyboardByTappingToView().
     */
    func isResettingAdjustmentEnabled(for scrollView: ODScrollView) -> Bool {
        true
    }
}

7 - 可选:当在多行 UITextInput 中键入时,您可以调整 ODScrollView 以匹配键盘的光标叠加。当 touchTextInputCursor(for UITextInput) 被触发时,需要调用 UITextInput 函数。

/**
## IMPORTANT:
This feature is not going to work unless textView is subView of _ODScrollView
*/
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
       _ODScrollView.trackTextInputCursor(for textView)
   return true
}