像键盘一样显示UIPickerView,没有UITextField

问题描述 投票:13回答:6

我正在寻找一种方法,当用户点击UIPickerView时显示UIBarButtonItem。想象一下用于表视图结果的过滤器。

[我知道我可以使用UITextField inputView,但事实并非如此-我只有UIBarButtonItemUITableView

我看过使用UIActionSheet,但看起来并不自然,特别是在显示动画时。

UIView动画在屏幕上和屏幕外的唯一选择吗?

该应用程序仅适用于iOS 6+和iPhone,因此我不需要保持与任何其他版本/习惯用法的兼容性。

ios ios6 uikit
6个回答
5
投票

这可能不是唯一的选择,但是动画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);
}];

20
投票

将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()

8
投票

一个更好的选择仍然是使用UITextField。您不必在屏幕上实际显示它。只需将其放在0x0 UIView中以使其不可见,将其inputView设置为UIPickerView,然后在其上调用becomeFirstResponder以显示选择器,并单击resignFirstResponder将其隐藏。

[UIView子类与UIPickerViewDelegateUIPickerViewDataSource方法一起实现所有子类很方便。


7
投票

至少对我来说,对于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)
}

结果看起来像这样:

enter image description here


6
投票

我接受了[[borisgolovnev


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.