我正在寻找一种方法,当用户点击UIPickerView
时显示UIBarButtonItem
。想象一下用于表视图结果的过滤器。
[我知道我可以使用UITextField
inputView,但事实并非如此-我只有UIBarButtonItem
和UITableView
。
我看过使用UIActionSheet,但看起来并不自然,特别是在显示动画时。
将UIView
动画在屏幕上和屏幕外的唯一选择吗?
该应用程序仅适用于iOS 6+和iPhone,因此我不需要保持与任何其他版本/习惯用法的兼容性。
这可能不是唯一的选择,但是动画UIPickerView应该相对容易些。添加选择器视图,使其显示在屏幕底部之外。当需要设置动画时:
[UIView animateWithDuration:0.3 animations:^{
self.datePicker.frame = CGRectMake(0, self.view.bounds.size.height - datePicker.bounds.size.height, datePicker.bounds.size.width, datePicker.bounds.size.height);
}];
以及何时该隐藏它:
[UIView animateWithDuration:0.3 animations:^{
self.datePicker.frame = CGRectMake(0, self.view.bounds.size.height, datePicker.bounds.size.width, datePicker.bounds.size.height);
}];
将UIPickerView作为已添加到视图的0大小的UITextField的inputView。
let picker = UIPickerView()
picker.dataSource = self
picker.delegate = self
let dummy = UITextField(frame: CGRectZero)
view.addSubview(dummy)
dummy.inputView = picker
dummy.becomeFirstResponder()
一个更好的选择仍然是使用UITextField
。您不必在屏幕上实际显示它。只需将其放在0x0 UIView
中以使其不可见,将其inputView
设置为UIPickerView
,然后在其上调用becomeFirstResponder
以显示选择器,并单击resignFirstResponder
将其隐藏。
[UIView
子类与UIPickerViewDelegate
和UIPickerViewDataSource
方法一起实现所有子类很方便。
至少对我来说,对于Swift4 + Autolayout解决方案,最简单的方法是不添加UITextField
,将UIView
作为UIPickerView
的容器,并将UIToolbar
作为顶视图/按钮的容器。
然后,如果需要,可以使用动画切换该容器视图的插入/偏移,以隐藏和取消隐藏拾取器。
PROPERTIES]
private lazy var view_PickerContainer: UIView = { let view = UIView() view.backgroundColor = .white view.addSubview(self.timePicker) view.addSubview(self.toolbar_Picker) return view }() private lazy var timePicker: UIDatePicker = { let picker = UIDatePicker() picker.minimumDate = Date() picker.datePickerMode = .time picker.setDate(Date(), animated: true) return picker }() private let timeFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "hh:mm a" return formatter }() private lazy var toolbar_Picker: UIToolbar = { let toolbar = UIToolbar() toolbar.barStyle = .blackTranslucent toolbar.barTintColor = .blueDarkText toolbar.tintColor = .white self.embedButtons(toolbar) return toolbar }()
我使用
SnapKit
以编程方式布置视图。您可以使用timePicker
的bounds.size作为布局的参考。
实施
在viewDidLoad()
// Setup timepicker and its container. self.view.addSubview(self.view_PickerContainer) self.view_PickerContainer.snp.makeConstraints { (make) in make.height.equalTo(self.timePicker.bounds.size.height + 50.0) make.leading.trailing.equalToSuperview() self.constraint_PickerContainerBottom = make.bottom.equalToSuperview().inset(-500.0).constraint } self.timePicker.snp.makeConstraints { (make) in make.height.equalTo(self.timePicker.bounds.size.height) make.width.equalToSuperview() make.bottom.equalToSuperview() } // Add toolbar for buttons. self.toolbar_Picker.snp.makeConstraints { (make) in make.height.equalTo(40.0) make.top.leading.trailing.equalToSuperview() }
嵌入顶视图
private func embedButtons(_ toolbar: UIToolbar) { func setupLabelBarButtonItem() -> UIBarButtonItem { let label = UILabel() label.text = "Set Alarm Time" label.textColor = .white return UIBarButtonItem(customView: label) } let todayButton = UIBarButtonItem(title: "Today", style: .plain, target: self, action: #selector(self.todayPressed(_:))) let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.donePressed(_:))) let flexButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) toolbar.setItems([todayButton, flexButton, setupLabelBarButtonItem(), flexButton, doneButton], animated: true) }
结果看起来像这样:
我接受了[[borisgolovnev