微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 14 中 UIPickerView 的背景颜色

如何解决iOS 14 中 UIPickerView 的背景颜色

UIPicker 上的背景似乎在 iOS 14 中发生了变化。我试图让它看起来与 iOS 13 中的一样,但我无法做到。以下是我现有的代码

func pickerView(_ pickerView: UIPickerView,viewForRow row: Int,forComponent component: Int,reusing view: UIView?) -> UIView {
    let pickerLabel = UIButton()
    let attributesForSelectedRow = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16,weight: .bold),NSAttributedString.Key.foregroundColor: UIColor.blue(),NSAttributedString.Key.backgroundColor: UIColor(rgb: 0xE5E5E5)]
            
    let attrTitle = NSMutableAttributedString(string: text,attributes: attributesForSelectedRow)
    pickerLabel.setAttributedTitle(attrTitle,for: .normal)
    pickerLabel.backgroundColor = UIColor(rgb: 0xE5E5E5)
    pickerLabel.contentHorizontalAlignment = .center
    return pickerLabel
}

我尝试过的解决方案:

func pickerView(_ pickerView: UIPickerView,reusing view: UIView?) -> UIView {
        
    let pickerLabel = UIButton.init(frame: CGRect(x: 0,y: 0,width: pickerView.frame.width,height: 36))

    if #available(iOS 14.0,*)
    {
       pickerView.subviews[safe:1]?.backgroundColor = UIColor.clear
    }
    
    let attributesForSelectedRow = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16,NSAttributedString.Key.backgroundColor: UIColor(rgb: 0xE5E5E5)]
            
    let attrTitle = NSMutableAttributedString(string: “M1”,for: .normal)
    pickerLabel.backgroundColor = UIColor(rgb: 0xE5E5E5)

    pickerLabel.contentHorizontalAlignment = .center
        
    return pickerLabel
}

我的解决方案的背景颜色看起来不错,但它不像 iOS 13 中那样采用 pickerview 的宽度。如何使灰色背景的宽度与选择器视图的宽度相同?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。