WWDC20: 更新 iOS 14 pickers, actions 及 menu 等控件
发布
更新
字数
441
阅读
3 分钟
阅读量
1299
iOS 14 很多原生控件的样式和功能都做了更新,带来了更好的体验,如新的 UIPageControl
特性支持不限数量、自定义背景等,新增的 UIColorPickerViewController
和 PHPickerViewController
,以及更新后的 UIDatePicker
和 menu 等。
几个比较突出的更新:
UIPageControl
- 无限制数量
- 自定义背景样式
- 自定义图标
let pageControl = UIPageControl()
pageControl.numberOfPages = 5
pageCcontrol.backgroundStyle = .prominent
pageControl.preferredIndicatorImage = UIImage(systemName: "bookmark.fill")
pageControl.setIndicatorImage(UIImage(system: "heart.fill", forPage: 0)
New UIColorPickerViewController
- 滴管选择器
- 网格布局
- 色盘
- 饱和度等调整
- 收藏颜色(可以在不同应用间共享使用)
var color = UIColor.blue
var colorPicker = UIColorPickerViewController
func pickColor() {
colorPicker.supportsAlpha = true
colorPicker.selectedColor = color
self.present(colorPicker, animated: true, completion: nil)
}
// Color picker delegates
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
color = viewController.selectedColor
}
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
//
}
UIDatePicker
精简样式用于显示,模态窗口可以选择日期、时间
let datePicker = UIDatePicker()
datePicker.date = Date(timeIntervalSinceReferenceDate:timeInterval)
// 设置为精简样式
datePicker.preferredDatePickerStyle = .compact
datePicker.calendar = Calendar(idnetifier: .japanese)
datePicker.datePickerMode = .date
New Photos Picker
- 支持搜索
- 多选
- 放大缩小
var configuration = PHPickerConfiguration()
// "unlimited" selection by specifying 0, default is 1
configuration.selectionLimit = 0
// Only show images (including Live Photos)
configuration.filter = .images
// Only videos or Live Photos (for their video complement), but no images
configuration.filter = .any(of: [.videos, .livePhotos])
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
// Handling results
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// The client is responsible for presentation and dismissal
picker.dismiss(animated: true)
// Get the first item provider from the results
let itemProvider = results.first?.itemProvider
// Access the UIImage representation for the result
if let itemProvider = itemProvider, itemProvider.canLoadObject(ofClass: UIImage.self) {
itemProvider.loadObject(ofClass: UIImage.self) { image, error in
if let image = image {
// Got the image
}
}
}
}
Menu
默认为长按触发
// 按钮添加菜单
button.menu = UIMenu(...)
// 自定义立即触发
button.showsMenuAsPrimaryAction = true
// UIBarButtonItem 添加菜单
barButtonItem.menu = UIMenu(...)
// 自定义触发
UIBarButtonItem(systemItem: .save, primaryAction: saveAction, menu: saveMenu)
UIBarButtonItem(image: optionsImage, menu: optionsMenu)
UIBarButtonItem(primaryAction: revertAction)