加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift通天遁地]一、超级工具-(18)创建强大、灵活的日期时间拾取

发布时间:2020-12-14 05:08:39 所属栏目:百科 来源:网络整理
导读:本文将演示如何制作强大、灵活的日期时间拾取器。 首先确保在项目中已经安装了所需的第三方库。 点击【Podfile】,查看安装配置文件。 1 platform :ios,‘ 12.0 ’ 2 use_frameworks! 3 4 target ‘ DemoApp ‘ do 5 source ‘ https://github.com/CocoaPods/

本文将演示如何制作强大、灵活的日期时间拾取器。

首先确保在项目中已经安装了所需的第三方库。

点击【Podfile】,查看安装配置文件。

1 platform :ios,‘12.02 use_frameworks!
3 
4 target DemoApp do
5     source https://github.com/CocoaPods/Specs.git
6     pod DateTimePicker
7 end

根据配置文件中的相关配置,安装第三方库。

然后点击打开【DemoApp.xcworkspace】项目文件。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

选择开始编写代码,创建一个日期时间拾取器。

 1 import UIKit
 2 //在当前类文件中,引入已经安装的第三方类库
 3 import DateTimePicker
 4 
 5 class ViewController: UIViewController {
 6     
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view,typically from a nib.
10         
11         //添加一个按钮,当用户点击该按钮时,弹出日期和时间拾取窗口,
12         let button = UIButton(frame: CGRect(x: 0,y: 80,width: 320,height: 40))
13         //设置按钮的背景颜色为橙色
14         button.backgroundColor = UIColor.orange
15         //设置按钮在正常状态下的标题文字
16         button.setTitle("Pick date and time",for: .normal)
17         //给按钮控件绑定点击事件
18         button.addTarget(self,19                          action: #selector(ViewController.showDateTimePicker),20                          for: .touchUpInside)
21         
22         //设置根视图的背景颜色为橙色
23         self.view.backgroundColor = UIColor.orange
24         //将按钮控件添加到根视图
25         self.view.addSubview(button)
26     }
27     
28     //添加一个方法,用来响应按钮的点击事件
29     func showDateTimePicker()
30     {
31         //创建两个常量
32         //1.日期的最小值为4天前
33         let min = Date().addingTimeInterval(-60 * 60 * 24 * 4)
34         //2.日期的最大值为4天后
35         let max = Date().addingTimeInterval(60 * 60 * 24 * 4)
36         
37         //通过最小日期和最大日期两个参数,初始化一个日期时间拾取器
38         let picker = DateTimePicker.show(minimumDate: min,maximumDate: max)
39         
40         //设置日期时间拾取器的背景颜色
41         picker.backgroundViewColor = UIColor(red: 51.0/255.0,42                                              green: 51.0/255.0,43                                              blue: 51.0/255.0,44                                              alpha: 0.5)
45         //设置日期时间拾取器的高亮颜色
46         picker.highlightColor = .orange
47         //设置日期时间拾取器的完成按钮的文字内容
48         picker.doneButtonTitle = "!! DONE DONE !!"
49         //设置日期时间拾取器的今日按钮的文字内容
50         picker.todayButtonTitle = "Today"
51         //当完成日期和时间的拾取时
52         picker.completionHandler = { date in
53             //在控制台输出日期和时间信息
54             print(date)
55         }
56     }
57 
58     override func didReceiveMemoryWarning() {
59         super.didReceiveMemoryWarning()
60         // Dispose of any resources that can be recreated.
61     }
62 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读