在iOS中增加选取器的大小-Swift 4

时间:2019-01-19 16:09:04

标签: ios uipickerview

我有一个看起来像这样的SB

enter image description here

产生此结果

我正在尝试增加选择器的字体。

关于此的最佳方法是什么?

这是我可以通过情节提要设置实现的东西吗?

我有这个

extension ScreenTimeViewController : UIPickerViewDelegate, UIPickerViewDataSource {

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return component == 0 ? 25 : 61
    }

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

        if view != nil {
            view?.layer.backgroundColor = UIColor.white.cgColor
            view?.layer.cornerRadius = (view?.bounds.width)! / 2

            return view!
        } else {

            let newView = UIView(frame: CGRect(x: 0, y: 0, width: 110, height: 40))

            newView.layer.masksToBounds = false
            newView.layer.backgroundColor = UIColor.white.cgColor
            newView.layer.cornerRadius = newView.bounds.width / 2

            switch component {
            case 0:
                addLabel(in: newView, at: CGPoint(x: newView.bounds.minX + 20, y: newView.bounds.midY), text: "\(row)", color: .black)
            case 1:
                addLabel(in: newView, at: CGPoint(x: newView.bounds.minX + 20, y: newView.bounds.midY), text: "\(row)", color: .black)
            default:
                break
            }

            return newView
        }

    }

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

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


}

1 个答案:

答案 0 :(得分:0)

尽管您可以在某种程度上自定义选择器的颜色,但是对于字体,如果您想要高度可定制的选择器,请参考github项目

https://github.com/mithyer/DatePicker-Swift

https://github.com/itsmeichigo/DateTimePicker