UIPickerView水平变化宽度问题

时间:2017-10-24 12:24:54

标签: ios swift uipickerview horizontal-scrolling

我正在尝试将UIPickerView从垂直设置为水平。 我已经旋转了UIPickerView和可重复使用的视图,但是我不能增加旋转的UIPickerView的宽度。

我知道我错过了一些东西但是因为我是iOS新手,我可以搞清楚。非常感谢

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

    @IBOutlet weak var pickerView: UIPickerView!
    let pickerData = ["pick a font","pick a font 1","pick a font 2","pick a font 3","pick a font 4","pick a font 4","pick a font 4","pick a font 4","pick a font 4","pick a font 4","pick a font 4"]
    let customWidth : CGFloat =  40
    let customHeight : CGFloat = 180

    override func viewDidLoad() {
        super.viewDidLoad()

        pickerView.transform = CGAffineTransform(rotationAngle: CGFloat(-90 * (Float.pi / 180)))
        //Trying to increase the width but this line is not doing anything.
        pickerView.frame = CGRect(x: -100, y: 0, width: view.frame.width + 200 , height: 30)
    }

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

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

    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return customHeight
    }
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return customWidth
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        let view = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: customHeight))
        view.transform = CGAffineTransform(rotationAngle: CGFloat(90 * (Float.pi / 180)))
        let label = UILabel(frame: CGRect(x: 0, y: customHeight / 2 - 15, width: 180
            , height: 30))

        label.text = pickerData[row]
        label.textColor = .white

        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 30, weight: .bold)
        view.addSubview(label)

        return view
    }
}

0 个答案:

没有答案