如何在自动布局中更改选择器视图的字体大小

how to change the font size of picker view in autolayout

我正在尝试修复我的选择器视图的自动布局,在 iPhone(紧凑宽度和常规高度)似乎没问题,但对于 iPad(常规宽度和常规高度),似乎我的选择器视图的字体大小与 iPhone 相同,我希望 iPad 中的选择器视图的字体大小稍微大一点,我通常会添加自定义的字体大小常规宽度和常规高度,但在界面构建器中没有调整选择器视图字体大小的选项。

我该怎么办?

这不能在 Interface Builder 中完成,您需要以编程方式完成。有点简单,像这样做:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    if UIDevice.current.userInterfaceIdiom == .pad {
        pickerLabel.font = UIFont.systemFont(ofSize: 16)
        pickerLabel.text = "Row \(row)"
    } else if UIDevice.current.userInterfaceIdiom == .phone {
        pickerLabel.font = UIFont.systemFont(ofSize: 14)
        pickerLabel.text = "Row \(row)"
    }
    return pickerLabel
}

如果您正在使用 pickerView titleForRow,您需要删除该功能才能使用 viewForRow