简体   繁体   中英

Swift - Cannot make PickerView Library to Work

I am trying to integrate this library in my project

https://github.com/filipealva/PickerView

As instructed, I added a UIView in the interface builder and created the @IBOutlet connection to the view.

Here is the code from my controller

import UIKit
import PickerView

class SignUpStep1ViewController: UIViewController {

    @IBOutlet weak var nextBtn: UIButton?
    @IBOutlet weak var dayPicker: PickerView!

    var months = [
        "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        dayPicker.delegate = self
        dayPicker.dataSource = self
        configureDayPicker()
        title = "BIRTH DATE"
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.nextBtn?.layer.cornerRadius = 22.5
    }

    @IBAction func nextBtnClicked(sender: UIButton) {
        performSegue(withIdentifier: "SignUpStep2ViewController", sender: self)
    }

    fileprivate func configureDayPicker() {
        print("DayPicker Configuration")
        dayPicker.scrollingStyle = .default
        dayPicker.backgroundColor = UIColor.red
        dayPicker.translatesAutoresizingMaskIntoConstraints = false
        dayPicker.scrollingStyle = PickerView.ScrollingStyle(rawValue: 1)!
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let backItem = UIBarButtonItem()
        backItem.title = ""
        navigationItem.backBarButtonItem = backItem
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension SignUpStep1ViewController: PickerViewDataSource {

    func pickerViewNumberOfRows(_ pickerView: PickerView) -> Int {
        return months.count
    }

    func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return months[index]
    }
}

extension SignUpStep1ViewController: PickerViewDelegate {

    func pickerViewHeightForRows(_ pickerView: PickerView) -> CGFloat {
        return 50.0
    }

    func pickerView(_ pickerView: PickerView, didSelectRow row: Int) {

    }

    func pickerView(_ pickerView: PickerView, didTapRow row: Int, index: Int) {

    }

    func pickerView(_ pickerView: PickerView, styleForLabel label: UILabel, highlighted: Bool) {
        if highlighted {
            label.font = UIFont.systemFont(ofSize: 25.0)
            label.textColor = view.tintColor
        } else {
            label.font = UIFont.systemFont(ofSize: 15.0)
            label.textColor = .lightGray
        }
    }
}

It shows that the IBOutlet connection is created in the controller, however the picker does not show in the view, what is going wrong here?

Here is the screenshot of storyboard view.

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

You are getting the red background color which means that the pickerview IS being shown (the red background is being set in configureDayPicker method). I think you are not getting the text because you are using index instead of row to access values from your array. Try changing

func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return months[index]
}

to

func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return months[row]
}

EDIT: Also, try setting your text color to be a contrast to confirm if the data is in fact being loaded (the tint color might be blending the text into the background)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM