UIPickerView中的UIVIew的背景颜色设置不正确

时间:2018-09-10 10:34:47

标签: ios iphone swift uipickerview picker

我正在尝试通过自定义设计实现UIPickerView。我在UIVIew内拍摄了UIPickerView,在UIVIew内拍摄了标签。选择器背景的颜色为黑色,现在我尝试将白色背景色应用于UIVIew并在View内进行标签。但是颜色不是我想要的。我想要纯白色,但是会返回灰色。有人可以帮我吗?

这是我的代码和输出:

if pickerView == pkr1 {
    let myView = UIView(frame: CGRect(x: 5, y: 0, width: pickerView.bounds.width - 10, height: Utilities.isDeviceiPad() ? 80.0 : 60.0))
    myView.backgroundColor = UIColor.white
    myView.isOpaque = false
    let myLabel = UILabel(frame: CGRect(x: 5, y: 0, width: myView.bounds.width - 10, height: myView.frame.height))
    myLabel.text = arrTemp[row]
    //  myLabel.center.y = myView.center.y
    myLabel.textAlignment = .center
    myLabel.numberOfLines = 2
    myLabel.backgroundColor = UIColor.white
    myLabel.alpha = CGFloat(1)
    myLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail

    myView.addSubview(myLabel)
    return myView
}

enter image description here

输出:

enter image description here

2 个答案:

答案 0 :(得分:0)

请通过以下方法将您的视图添加到选择器视图中,并进行检查...希望它将起作用。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal">
<TextView
    android:id="@+id/textView_option"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="@string/option"
    android:layout_margin="15dp"
    android:textSize="20dp"
    app:layout_constraintEnd_toStartOf="editText_option"
    app:layout_constraintStart_toStarOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
/>

<EditText
    android:id="@+id/editText_option"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:lines="1"
    app:layout_constraintEnd_toStartOf="textView_option"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintStart_toEndOf="@+id/imageButtonOption"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
  />

<ImageButton
     android:id="@+id/imageButtonOption"
     android:layout_width="0dp"
     android:layout_height="0dp"
    android:src="@drawable/icon_minus"
    android:scaleType="fitCenter"
    android:background="@null"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintLeft_toRightOf="@+id/textView_option"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"

 />
 </android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

所选视图透明的原因是保存所有内容的UIPickerView子视图使用CAGradientLayer作为蒙版来实现深度效果。由于某种原因,Apple决定也使选定的视图透明。我们不能简单地摆脱那个面具,因为我们会失去那种幻想的效果。因此,我们需要进一步了解该梯度。

这是此渐变的外观。我添加了Alpha通道值,以便您可以看到此渐变的行为。

UIPickerView gradient layer mask

如您所见,它有6种颜色,除了中间的两个值之外,其他一切都很好。它们是0.8而不是1.0,这使您选择的视图透明。

这是一种非常糟糕的方法,我强烈不建议这样做,但是如果您无论如何都需要使选定的项目不透明,则可以简单地为您创建一个子类{{1} },然后像这样更改渐变。

UIPickerView

希望它对您有帮助。

相关问题