【问题标题】:Can you get rid of opaque rectangle in a UIPickerView?你能摆脱 UIPickerView 中的不透明矩形吗?
【发布时间】:2021-11-12 14:18:38
【问题描述】:

我想删除 UIPickerView 中的这个灰色框。

我正在尝试重新创建它。

如果有办法,我将不胜感激。

这是代码 sn-p。我不精通 UIKit,但如果有任何可以改进的地方,我也会喜欢提示。我知道 SwiftUI 没有 UIKit 的自定义功能,这就是我使用 UIKit 的原因。

var data = ["23","24","25","26","27","28","29"]

struct CustomPicker: UIViewRepresentable {
    @Binding var selected: String
    func makeCoordinator() -> Coordinator {
        return CustomPicker.Coordinator(parent1: self)
    }
    
    
    func makeUIView(context: UIViewRepresentableContext<CustomPicker>) -> UIPickerView {
        let picker = UIPickerView()
        
        picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        picker.dataSource = context.coordinator
        picker.delegate = context.coordinator
        return picker
    }

    func updateUIView(_ uiView: UIPickerView, context: UIViewRepresentableContext<CustomPicker>) {
        
    }
    
    class Coordinator: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
        var parent: CustomPicker
        init(parent1: CustomPicker) {
            parent = parent1
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return data.count
        }
        
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
        

        func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
            
            let view = UIView(frame: CGRect(x: 0, y: 0, width: 155, height: 77))
            
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
            
            label.text = data[row]
            label.textColor = UIColor(#colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1))
            label.textAlignment = .center
            label.font = .systemFont(ofSize: 40, weight: .regular)
                       
            view.backgroundColor = .clear
            
            view.addSubview(label)
            
            view.clipsToBounds = true
            view.layer.cornerRadius = view.bounds.height / 2
            
            //Border Color
//            view.layer.borderWidth = 2.5
//            view.layer.borderColor = UIColor.white.cgColor
            return view
        }
        
        func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
           
            return 155
        }
        
        //Height of row
        func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
            return 77
        }
        
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            self.parent.selected = data[row]
        }
        
    }
}

【问题讨论】:

    标签: swiftui uikit uipickerview


    【解决方案1】:

    添加以下内容

    pickerView.subviews[1].alpha = 0
    

    进入UIPickerView协调器方法

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    .
    .
    .
    pickerView.subviews[1].alpha = 0
    .
    .
    .
    }
    

    这将在选择器视图中检索索引为1(即灰色条)的子视图,并将alpha 的值设置为0,从而生成以下屏幕截图。

    注意:这是添加到协调器中的,因为在 makeUIView 方法中访问子视图会导致应用程序在子视图未布局时崩溃,从而导致 subviews 数组具有0的长度

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 2014-10-11
      • 2021-12-28
      • 2016-05-25
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      相关资源
      最近更新 更多