你能摆脱 UIPickerView 中的不透明矩形吗?
Can you get rid of opaque rectangle in a UIPickerView?
我想删除 UIPickerView 中的这个灰色框。
我正在尝试重新创建它。
如果有办法,我将不胜感激。
这是代码片段。我不精通 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]
}
}
}
添加以下内容
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
的数组
我想删除 UIPickerView 中的这个灰色框。
我正在尝试重新创建它。
如果有办法,我将不胜感激。
这是代码片段。我不精通 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]
}
}
}
添加以下内容
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