UITextField-Navigation 4.2

UITextField-Navigation 4.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2019 年 4 月
SPM支持 SPM

Thanh Pham 维护。




  • Thanh Pham
'           __________________ _______          _________ _______ _________ _______  _        ______  
'  |\     /|\__   __/\__   __/(  ____ \|\     /|\__   __/(  ____ \\__   __/(  ____ \( \      (  __  \ 
'  | )   ( |   ) (      ) (   | (    \/( \   / )   ) (   | (    \/   ) (   | (    \/| (      | (  \  )
'  | |   | |   | |      | |   | (__     \ (_) /    | |   | (__       | |   | (__    | |      | |   ) |
'  | |   | |   | |      | |   |  __)     ) _ (     | |   |  __)      | |   |  __)   | |      | |   | |
'  | |   | |   | |      | |   | (       / ( ) \    | |   | (         | |   | (      | |      | |   ) |
'  | (___) |___) (___   | |   | (____/\( /   \ )   | |   | )      ___) (___| (____/\| (____/\| (__/  )
'  (_______)\_______/   )_(   (_______/|/     \|   )_(   |/       \_______/(_______/(_______/(______/ 
'                                                                                                     
'   _        _______          _________ _______  _______ __________________ _______  _                
'  ( (    /|(  ___  )|\     /|\__   __/(  ____ \(  ___  )\__   __/\__   __/(  ___  )( (    /|         
'  |  \  ( || (   ) || )   ( |   ) (   | (    \/| (   ) |   ) (      ) (   | (   ) ||  \  ( |         
'  |   \ | || (___) || |   | |   | |   | |      | (___) |   | |      | |   | |   | ||   \ | |         
'  | (\ \) ||  ___  |( (   ) )   | |   | | ____ |  ___  |   | |      | |   | |   | || (\ \) |         
'  | | \   || (   ) | \ \_/ /    | |   | | \_  )| (   ) |   | |      | |   | |   | || | \   |         
'  | )  \  || )   ( |  \   /  ___) (___| (___) || )   ( |   | |   ___) (___| (___) || )  \  |         
'  |/    )_)|/     \|   \_/   \_______/(_______)|/     \|   )_(   \_______/(_______)|/    )_)         
'                                                                                                     

UITextField-Navigation

CI Status GitHub issues Codecov Documentation

GitHub release Platform License

Carthage

CocoaPods

Say thanks!

描述

UITextField-Navigation 为您的 UITextFieldUITextView 添加了上一步、下一步和完成按钮。它允许您在 Interface Builder 中或通过程序指定下一步字段。然后,您可以轻松访问每个 UITextFieldUITextView 的下一步和上一步字段。

UI 可以进行高度自定义。支持从右到左的语言。

要运行示例项目

pod try UITextField-Navigation

Screenshot 0 Screenshot 1 Screenshot 2

使用方法

基础

您可以为每个 UITextFieldUITextView 设置 nextNavigationField 属性,无论是通过接口构建工具还是通过编程方式。previousNavigationField 属性将自动为您设置在另一个 UITextFieldUITextView 上。

示例

import UITextField_Navigation

...
let textField = UITextField()
let textView = UITextView()
textField.nextNavigationField = textView

assert(textView == textField.nextNavigationField)
assert(textField == textView.previousNavigationField)

请注意,nextNavigationFieldpreviousNavigationField 属性不会被保留。

捕获点击

要捕获在“下一步”、“上一步”和“完成”按钮上的点击事件,请将 delegate 分配给您的 NavigationField,它是一个 UITextFieldUITextView,也可以通过接口构建工具或编程方式完成。然后实现 NavigationFieldDelegate 协议(除了实现 UITextFieldDelegateUITextViewDelegate 协议)。请注意,您必须显式声明 delegate 符合 NavigationFieldDelegate 协议以使它起作用。

Swift
import UIKit
import UITextField_Navigation

...
extension ViewController: NavigationFieldDelegate { // explicitly protocol conforming declaration

    func navigationFieldDidTapPreviousButton(_ navigationField: NavigationField) {
        navigationField.previousNavigationField?.becomeFirstResponder()
        // your custom work
    }

    func navigationFieldDidTapNextButton(_ navigationField: NavigationField) {
        navigationField.nextNavigationField?.becomeFirstResponder()
        // your custom work
    }

    func navigationFieldDidTapDoneButton(_ navigationField: NavigationField) {
        navigationField.resignFirstResponder()
        // your custom work
    }
}
Objective-C
@import UITextField_Navigation;
#import "ViewController.h"

@interface ViewController () <NavigationFieldDelegate> // explicitly protocol conforming declaration

...
#pragma mark - NavigationFieldDelegate

- (void)navigationFieldDidTapPreviousButton:(id<NavigationField>)navigationField {
    [navigationField.previousNavigationField becomeFirstResponder];
    // your custom work
}

- (void)navigationFieldDidTapNextButton:(id<NavigationField>)navigationField {
    [navigationField.nextNavigationField becomeFirstResponder];
    // your custom work
}

- (void)navigationFieldDidTapDoneButton:(id<NavigationField>)navigationField {
    [navigationField resignFirstResponder];
    // your custom work
}

设置按钮标题

可以分别修改每个实例的前进、后退和完成按钮的标题,只需设置相应按钮的 title 属性。

navigationField.navigationFieldToolbar?.previousButton.title = "Previous"
navigationField.navigationFieldToolbar?.nextButton.title = "Next"
navigationField.navigationFieldToolbar?.doneButton.title = "Dismiss"

或者,可以使用 Config 全局设置所有实例的标题。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UITextField_Navigation.Config.previousButtonTitle = "Previous"
    UITextField_Navigation.Config.nextButtonTitle = "Next"
    UITextField_Navigation.Config.doneButtonTitle = "Done"
    return true
}

UI 自定义

使用 UIAppearance

修改 NavigationFieldToolbarNavigationFieldToolbarButtonItem 类的外观代理,以自定义导航视图中所有字段的 UI。

NavigationFieldToolbar.appearance().barStyle = .black
NavigationFieldToolbar.appearance().backgroundColor = .purple
if #available(iOS 11.0, *) {
    UIButton.appearance(whenContainedInInstancesOf: [NavigationFieldToolbar.self]).tintColor = .white
} else {
    NavigationFieldToolbarButtonItem.appearance().tintColor = .white
}

Screenshot 3

直接修改并添加更多按钮

或者,您可以通过访问 UITextFieldUITextViewnavigationFieldToolbar 属性来直接修改每个导航视图的 UI。

...
navigationField.navigationFieldToolbar?.barStyle = .default
navigationField.navigationFieldToolbar?.backgroundColor = .red

// Add a custom button
let customButton = UIBarButtonItem(title: "Custom", style: .plain, target: nil, action: nil)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
navigationField.navigationFieldToolbar?.items = [navigationField.navigationFieldToolbar!.previousButton, navigationField.navigationFieldToolbar!.nextButton, customButton, flexibleSpace, navigationField.navigationFieldToolbar!.doneButton]

Screenshot 4

安装

Carthage

将以下行添加到您的Cartfile中

github "T-Pham/UITextField-Navigation"

CocoaPods

将以下行添加到您的Podfile中

pod 'UITextField-Navigation'

手动

  1. 下载并将 /UITextField-Navigation/Classes 文件夹拖放到您的项目中。
  2. 恭喜!

兼容性

  • Swift 4, 5:请使用最新版本
  • Swift 3:请使用版本 3.0.0
  • Swift 2:请使用版本 1.4.3

使用UITextField-Navigation的应用

大家好。我看到一些应用已经使用了这个库。如果您的应用也使用了这个库,请在下面分享它。请将它添加到以下列表中。谢谢!

  1. Catder - 随机动画猫图片
  2. Gradus - 一个评分计算器

协议

UITextField-Navigation可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。