无法在iOS的uipicker视图中滚动?

时间:2018-06-29 06:47:49

标签: ios swift uipickerview

我已经为选择器视图创建了XIB文件,现在点击手势时,我将在超级视图中添加选择器视图。它被成功添加,但是我无法滚动。我还添加了两个用于取消和完成的按钮,但未检测到这些按钮的任何操作。请让我知道问题是什么

这是用于添加uipicker视图的代码。

@objc  func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {


    let frame:CGRect = CGRect(x: 0, y: self.view.frame.height + 200, width: self.view.frame.width, height: 200)
    self.itemPicker = ItemPicker.instanceFromNib(with: frame) as? ItemPicker
    self.itemPicker?.configureView(frame: frame)
    self.view.addSubview(self.itemPicker!)
    self.itemPicker?.itemPickerDelegate = self
    showView()
}

func showView() {
    UIView.animate(withDuration: 0.2) {

        self.itemPicker?.frame = CGRect(x: 0, y: self.view.frame.height - 200, width: self.view.frame.width, height: 200)
    }
}

func hideView() {
    UIView.animate(withDuration: 0.2) {
        self.itemPicker?.frame = CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: 200)

    }
}

类选择器视图的代码

protocol ItemPickerDelegate {

    func pickerCancelled()
    func pickerDone()
    func itemPicked(with item:String)

}

class ItemPicker: UIPickerView {

    /// IBOutlets
    @IBOutlet weak var pickerView: UIPickerView!
    var itemPickerDelegate:ItemPickerDelegate!
    var arrItems:[String] = ["Item1","Item2","Item3","Item5","Item6"]

    class func instanceFromNib(with frame:CGRect) -> UIView {
        let view = Bundle.main.loadNibNamed(Titles.XIB.itemPicker, owner: self, options: nil)! [0] as! UIView
        return view
    }

    func configureView(frame:CGRect) {
        self.frame = frame
        self.pickerView.delegate = self
        self.pickerView.dataSource = self
    }

    //MARK:IBActions
    @IBAction func actionCancel(_ sender: Any) {
        DILog.print(items: "actionCancel")
        self.itemPickerDelegate.pickerCancelled()
    }

    @IBAction func actionDone(_ sender: Any) {
        DILog.print(items: "actionDone")
        self.itemPickerDelegate.pickerDone()
    }
}

extension ItemPicker:UIPickerViewDelegate {


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return arrItems[row]
    }
}


extension ItemPicker:UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {

        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return arrItems.count

    }

}

请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:3)

查看控制器类

import UIKit

class StackViewClass: UIViewController {

    /// View in which Picker Subview will be added
    var baseView : UIView?

    /// Picker View Class object
    var customPickerView : ItemPicker?

    /// Gesture which is used to show the picker in subview
    var tapGesture : UITapGestureRecognizer?

    override func viewDidLoad() {
        super.viewDidLoad()
        /// Assigning Tap gesture
        tapGesture = UITapGestureRecognizer()
        tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleViewTapGesture(_:)))        
    }

    override func viewDidAppear(_ animated: Bool) {
        /// Adding Base View in Main View
        baseView = UIView()
        baseView?.backgroundColor = UIColor.lightGray
        baseView?.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(baseView!)
        baseView?.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        baseView?.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        baseView?.widthAnchor.constraint(equalToConstant: 250).isActive = true
        baseView?.heightAnchor.constraint(equalToConstant: 150).isActive = true

        /// Adding Gesture
        baseView?.addGestureRecognizer(tapGesture!)
    }

    /// Tap Gesture Handler
    @objc func handleViewTapGesture(_ sender: UITapGestureRecognizer) {
        customPickerView = ItemPicker()
        customPickerView?.frame = CGRect(x: 0, y: sender.view!.frame.maxY, width: sender.view!.frame.size.width, height: sender.view!.frame.size.height)
        sender.view?.addSubview(customPickerView!)
        self.showView(sender.view!)
    }

    /// Function which is used to show Picker in base View
    func showView(_ sender: UIView) {
        customPickerView?.removeFromSuperview()
        sender.addSubview(customPickerView!)
        UIView.animate(withDuration: 0.2) {
            self.customPickerView?.frame = CGRect(x: 0, y: 0, width: sender.frame.size.width, height: sender.frame.size.height)
        }
    }
}

项目选择器视图XIB enter image description here

您的项目选择器类别

import UIKit

protocol ItemPickerDelegate {

    func pickerCancelled()
    func pickerDone()
    func itemPicked(with item:String)

}

/// Assign Class to UIView
class ItemPicker: UIView {

    /// After assigning FileOwner to ItemPicker Class
    /// Create ContentView Outlet
    @IBOutlet var contentView: UIView!

    /// IBOutlets
    @IBOutlet weak var pickerView: UIPickerView!

    /// Arrays
    var itemPickerDelegate:ItemPickerDelegate!
    var arrItems:[String] = ["Item1","Item2","Item3","Item5","Item6"]

    /// Frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadNib()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadNib()

    }

    /// I am Here loading Nib names as "ItemPicker"
    func loadNib() {
        Bundle.main.loadNibNamed("ItemPicker", owner: self, options: [:])
        // 2. Adding the 'contentView' to self (self represents the instance of a WeatherView which is a 'UIView').
        addSubview(contentView)
        // 3. Setting this false allows us to set our constraints on the contentView programtically
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.backgroundColor = UIColor.clear
        // 4. Setting the constraints programatically
        contentView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        contentView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        contentView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true

        pickerView.delegate = self
        pickerView.dataSource = self
    }

    func configureView(frame:CGRect) {
        self.frame = frame
        self.pickerView.delegate = self
        self.pickerView.dataSource = self
    }

    //MARK:IBActions
    @IBAction func actionCancel(_ sender: Any) {
       // DILog.print(items: "actionCancel")
        self.itemPickerDelegate.pickerCancelled()
    }

    @IBAction func actionDone(_ sender: Any) {
       // DILog.print(items: "actionDone")
        self.itemPickerDelegate.pickerDone()
    }
}

extension ItemPicker:UIPickerViewDelegate
{
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return arrItems[row]
    }
}


extension ItemPicker:UIPickerViewDataSource
{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return arrItems.count
    }
}

模拟器输出

enter image description here

驱动器链接 https://drive.google.com/open?id=14ThRCsXXGMr6ckqaC8mxTiyDj4HrGkXu

答案 1 :(得分:0)

您尝试过

view.bringSubview(toFront: yourView)