CountryPickerView 3.3.0

CountryPickerView 3.3.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最后发布2021年10月
SPM支持Swift Package Manager

Kizito Nwose维护。



  • Kizito Nwose

CountryPickerView

Build Status Platform Version Carthage compatible SPM compatible License

CountryPickerView是一个简单的、可定制的iOS应用中选择国家的视图。

你可以 Clone/下载 仓库并运行 示例项目 以查看 CountryPickerView 的实际效果。首先从 CountryPickerViewDemo 目录运行 pod install

安装

请注意,3.x 版本的发布与 Swift 5 兼容。对于 Swift 4 兼容,请使用 2.x 版本的发布。对于 Swift 3 兼容,请使用 1.x 版本的发布。

Swift 包管理器

Swift 包管理器 是嵌入在 Xcode 中的依赖关系管理器。

如果你使用 Xcode 11 或更高版本,请转到 文件 / Swift 包 / 添加包依赖,并输入包仓库 URL https://github.com/kizitonwose/CountryPickerView.git,然后遵循说明。

CocoaPods

CountryPickerView可以通过CocoaPods获取。只需将以下内容添加到您的Podfile中即可:

use_frameworks!

target '<Your Target Name>' do
  pod 'CountryPickerView'
end

Carthage

Carthage是一个去中心化的依赖管理器,它会构建您的依赖,并提供二进制框架。

要通过Carthage安装CountryPickerView,只需将以下内容添加到您的Cartfile中:

github "kizitonwose/CountryPickerView"

手动安装

  1. 将CountryPickerView仓库放在您项目目录的某个位置。
  2. 在Xcode中,将CountryPickerView.xcodeproj添加到您的项目中。
  3. 在您的应用程序目标上,将CountryPickerView框架添加为嵌入的二进制文件。
    1. 在“通用”选项卡中。
    2. 在“构建阶段”选项卡中作为目标依赖项。

使用方法

如果您正在使用Storyboards/Interface Builder,可以在Storyboard中添加一个UIView来创建CountryPickerView实例,然后在“控件”部分(右侧面板)的“身份检查器”选项卡的“自定义类”字段中手动将视图的类更改为CountryPickerView。

您还可以通过编程方式创建CountryPickerView实例。

import CountryPickerView

let cpv = CountryPickerView(frame: /**Desired frame**/)

要获取您CountryPickerView实例在任何时候所选的国家,请使用selectedCountry属性。

let country = cpv.selectedCountry
print(country)

此属性不是可选的,默认值为用户的当前国家,由设备的当前Locale派生。

自定义

通过CountryPickerView实例直接提供视图的自定义选项,而通过CountryPickerViewDataSource协议提供内部CountryPicker表格视图的选项。如果要通知用户从列表中选择国家,则还需要设置CountryPickerViewDelegate协议。

import CountryPickerView

class DemoViewController: UIViewController, CountryPickerViewDelegate, CountryPickerViewDataSource {

    @IBOutlet weak var countryPickerView: CountryPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        countryPickerView.delegate = self
        countryPickerView.dataSource = self
    }
}

CountryPickerView 属性

属性 说明 默认值
showCountryCodeInView 在视图中显示或隐藏国家代码(例如 NG)。
showPhoneCodeInView 在视图中显示或隐藏电话代码(例如 +234)。
showCountryNameInView 在视图中显示或隐藏国家名称(例如 尼日利亚)。
font 电话/国家代码文本的字体。 系统字体
textColor 电话/国家代码文本的颜色。 黑色
flagSpacingInView 国旗图像和电话代码文本之间的间距。 8px
hostViewController 用于显示内部 CountryPickerViewController 的视图控制器。如果是 UINavigationController 的实例,则 CountryPickerViewController 将压入堆栈。如果不是,则将在自己的导航堆栈中呈现 CountryPickerViewController。如果此属性为 nil,则视图将尝试找到最近的视图控制器并使用该控制器呈现或压入 CountryPickerViewController nil
delegate CountryPickerViewDelegate 类型的实例。 nil
dataSource CountryPickerViewDataSource 类型的实例。 nil

注意:属性 showCountryCodeInViewshowCountryNameInView 不能同时启用。启用其中一个将禁用另一个。您只能显示列表上的所有属性(请参阅 CountryPickerViewDataSource)。

CountryPickerViewDelegate

  • 当用户从列表中选择国家或手动设置 CountryPickerViewselectedCountry 属性时调用。

    func countryPickerView(_ countryPickerView: CountryPickerView, didSelectCountry country: Country)
  • 在呈现或压入 CountryPickerViewController 之前调用。CountryPickerViewController 是 UITableViewController 的子类。

    func countryPickerView(_ countryPickerView: CountryPickerView, willShow viewController: CountryPickerViewController)
  • CountryPickerViewController 呈现或压入之后调用。CountryPickerViewController 是 UITableViewController 的子类。

    func countryPickerView(_ countryPickerView: CountryPickerView, didShow viewController: CountryPickerViewController)

注意:如果您在项目中已经有了一个 Country 类或结构体,实现 didSelectCountry 委托方法可能会引起编译错误,错误信息表示您的派生类不符合 CountryPickerViewDelegate 协议。这是因为 Xcode 无法确定要使用哪个 Country 模型。解决办法是将方法签名中的 Country 替换为 typealias CPVCountry,现在您的委托方法应如下所示

func countryPickerView(_ countryPickerView: CountryPickerView, didSelectCountry country: CPVCountry)

您还可以使用 CPVCountry 作为框架 Country 模型的替代品,并在项目的其他部分使用它。

此外,willShowdidShow 代理方法都是可选的。如果 CountryPickerViewController 被显示(不是推送),它会被嵌入到 UINavigationController 中。CountryPickerViewController 类被提供,以便您在需要时自定义其外观。您还可以访问 CountryPickerViewController 中的公共 searchController(UISearchController) 属性以进行自定义。

CountryPickerViewDataSource

数据源方法定义了内部(国家列表)ViewController 的行为。运行演示项目以探索选项。所有方法都是可选的。

  • 一个国家数组,您希望显示在列表顶部。这对于针对特定人群的应用很有用。

    func preferredCountries(in countryPickerView: CountryPickerView) -> [Country]
  • 所需的首选部分的标题。

    func sectionTitleForPreferredCountries(in countryPickerView: CountryPickerView) -> String?

    注意:如果要在列表中显示首选国家,您必须从 preferredCountries(in countryPickerView: CountryPickerView) 返回一个非空的国家数组以及该部分的标题。仅返回数组或标题将不会起作用。

  • 只显示列表上的 首选国家部分。默认值是 false

    func showOnlyPreferredSection(in countryPickerView: CountryPickerView) -> Bool

    返回 true 以隐藏内部列表,使您的用户只能从首选国家列表中进行选择。

  • 所需的国家列表部分标题标签的字体。可用于配置文本大小。

    func sectionTitleLabelFont(in countryPickerView: CountryPickerView) -> UIFont
  • 所需的国家列表部分标题标签的文本颜色。

    func sectionTitleLabelColor(in countryPickerView: CountryPickerView) -> UIColor?
  • 所需的国家列表单元标签的字体。可用于配置文本大小。

    func cellLabelFont(in countryPickerView: CountryPickerView) -> UIFont
  • 所需的国家列表国家名称的文本颜色。

    func cellLabelColor(in countryPickerView: CountryPickerView) -> UIColor?
  • 所需的国家列表旗帜图片的尺寸。

    func cellImageViewSize(in countryPickerView: CountryPickerView) -> CGSize
  • 所需的国家列表旗帜图片的圆角半径。

    func cellImageViewCornerRadius(in countryPickerView: CountryPickerView) -> CGFloat
  • 当内部视图控制器被推送/显示时的导航项标题。默认值是 nil

    func navigationTitle(in countryPickerView: CountryPickerView) -> String?
  • 如果内部视图控制器被显示(不是推送),则用作导航项按钮的按钮。如果返回 nil,则使用默认的 "关闭" 按钮此方法仅允许您返回一个按您想要定制的按钮。默认值是 nil

    func closeButtonNavigationItem(in countryPickerView: CountryPickerView) -> UIBarButtonItem?

    注意:与该按钮关联的任何 targetaction 都将替换为该按钮的唯一目的是关闭内部视图控制器。

  • 所需搜索栏的位置。默认值是 .tableViewHeader

    func searchBarPosition(in countryPickerView: CountryPickerView) -> SearchBarPosition

    可能的值:.tableViewHeader.navigationBar.hidden

  • 在列表上显示与国家名称并列的电话区号。例如尼日利亚(+234)。默认值是 false

    func showPhoneCodeInList(in countryPickerView: CountryPickerView) -> Bool
  • 在列表上显示与国家名称并列的国家代码。例如尼日利亚(NG)。如果 true,搜索也将针对国家代码执行。默认值是 false

    func showCountryCodeInList(in countryPickerView: CountryPickerView) -> Bool
  • 在列表上显示所选国家的勾选标记。默认值是 true

    func showCheckmarkInList(in countryPickerView: CountryPickerView) -> Bool
  • 用于在列表上显示国家名称的本地化。默认值为当前本地化。

    func localeForCountryNameInList(in countryPickerView: CountryPickerView) -> Locale

使用 CountryPickerView 与 UITextField

CountryPickerView 的一个很好的用例是将其用作电话号码输入框的左侧视图。

class DemoViewController: UIViewController {

    @IBOutlet weak var phoneNumberField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let cpv = CountryPickerView(frame: CGRect(x: 0, y: 0, width: 120, height: 20))
        phoneNumberField.leftView = cpv
        phoneNumberField.leftViewMode = .always
    }
}

这意味着用户无需在文本字段中输入国家的电话区号。这还确保您从 CountryPickerView 获得有效的电话区号,而不是依赖于用户。

独立使用内部选择器

如果您不想显示默认视图或者您有自己的实现来显示国家信息,您仍然可以使用内部选择器通过调用 CountryPickerView 实例上的 showCountriesList(from: UIViewController) 方法,允许用户从列表中选择国家。

重要提示:保留对 CountryPickerView 实例的字段引用,否则它将被垃圾收集,任何尝试使用它的行为都会导致崩溃。

class DemoViewController: UIViewController {

    // Keep a field reference
    let countryPickerView = CountryPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func buttonPressed(_ sender: Any) {
        countryPickerView.showCountriesList(from: self)
    }
}

在上述示例中,调用 countryPickerView.showCountriesList(from: self) 将导致内部选择器视图控制器以其自己的导航堆栈呈现,因为 DemoViewController 不是一个导航控制器。

如果您已有导航堆栈,可以通过调用 countryPickerView.showCountriesList(from: self.navigationController!) 或以安全的方式进行。

if let nav = self.navigationController {
	countryPickerView.showCountriesList(from: nav)
}

别忘了设置委托来通知用户从列表中选择国家。内部选择器视图控制器的使用示例包含在演示项目中。

创建国家实例

您可以使用 CountryPickerView 类中的以下任意方法来创建 Country 实例。

let countryPickerView = CountryPickerView()
let country = countryPickerView.getCountryByName("Nigeria")
let country2 = countryPickerView.getCountryByCode("NG")
let country3 = countryPickerView.getCountryByPhoneCode("+234")

您还可以使用这些辅助方法设置所选国家。

let countryPickerView = CountryPickerView()
countryPickerView.setCountryByName("Nigeria")
countryPickerView.setCountryByCode("NG")
countryPickerView.setCountryByPhoneCode("+234")

许可证

CountryPickerView 是在 MIT 许可下分发的。有关详细信息,请参阅 LICENSE