【问题标题】:Increase the Size of Picker in iOS - Swift 4在 iOS 中增加 Picker 的大小 - Swift 4
【发布时间】:2019-03-14 09:31:42
【问题描述】:

我有一个像这样的SB

它产生这个结果

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

最好的方法是什么?

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

我有这个

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
    }


}

【问题讨论】:

  • 您需要将选取器视图水平拉伸一点,然后设置其字体。看到这个问题:stackoverflow.com/questions/44223862/…
  • @Sweeper 答案显示 Swift3,我正在寻找 Swift 4 中的东西。
  • 我相信它并没有太大变化。只需使用该问题中的代码即可。如果有错误,Xcode 会建议修复。
  • @Sweeper 我尝试了你建议的帖子中的代码,我得到了错误:i.imgur.com/QwCDSzq.png
  • 我并不是说你应该直接复制和粘贴它...<Data Array> 是集合视图中字符串数组的占位符。在这种情况下,您可以将整个 <Data Array>[row] 替换为 "\(row)",因为您显示的是数字 0 到 59。

标签: ios uipickerview


【解决方案1】:

虽然你可以在一定程度上自定义选择器的颜色,但是如果你想要一个高度可定制的选择器,请参考 github 项目,对于字体没有可用的选项

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

https://github.com/itsmeichigo/DateTimePicker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2018-02-10
    相关资源
    最近更新 更多