UITextField阴影不会显示

时间:2016-12-27 13:41:48

标签: ios swift swift3

在我的xib文件中,我创建了一个通用函数,我希望通过为这些文本字段实现阴影来添加底部边框

func setupTextField(textField: UITextField) {

        textField.leftViewMode = UITextFieldViewMode.always
        let imageView = UIImageView();
        let image = UIImage(named: "calendar");
        imageView.image = image;
        imageView.frame = CGRect(x: 5, y: 8, width: 20, height: 20)
        textField.addSubview(imageView)
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: textField.frame.height))
        textField.leftView = paddingView


        textField.borderStyle = .none
        textField.layer.backgroundColor = UIColor.white.cgColor
        textField.layer.masksToBounds = false
        textField.layer.shadowColor = UIColor(hex: "#D8D8D8").cgColor
        textField.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        textField.layer.shadowOpacity = 1.0
        textField.layer.shadowRadius = 0.0


    }

我已经通过我的awakeFromNib()方法调用了此函数,因此它在我的第二个UITextField上添加了阴影,但在第一个UITextField上没有。虽然从调试中我可以看到,如果没有第二个mPDF error: IMAGE Error (http://xxxx.com/image.png): Error parsing image file - image type not recognised, and not supported by GD imagecreate,那么这将在我的第一个上画一个阴影。

如何克服这个问题?

2 个答案:

答案 0 :(得分:1)

有关Swift 4,Swift 4.2,Swift 5,请参阅以下答案 可以给任何UIView子类阴影(imageview,label,textfield,textview等)

extension UIView {
    /* The color of the shadow. Defaults to opaque black. Colors created
     * from patterns are currently NOT supported. Animatable. */
    @IBInspectable var shadowColor: UIColor? {
        set {
            layer.shadowColor = newValue!.cgColor
        }
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            else {
                return nil
            }
        }
    }

    /* The opacity of the shadow. Defaults to 0.4 Specifying a value outside the
     * [0,1] range will give undefined results. Animatable. */
    @IBInspectable var shadowOpacity: Float {
        set {
            layer.shadowOpacity = newValue
        }
        get {
            return layer.shadowOpacity
        }
    }

    /* The shadow offset. Defaults to (1, 2). Animatable. */
    @IBInspectable var shadowOffset: CGPoint {
        set {
            layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
        }
        get {
            return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
        }
    }

    /* The blur radius used to create the shadow. Defaults to 3. Animatable. */
    @IBInspectable var shadowRadius: CGFloat {
        set {
            layer.shadowRadius = newValue
        }
        get {
            return layer.shadowRadius
        }
    }
}

答案 1 :(得分:0)

文本框阴影 迅速4.2

textFieldDOB.backgroundColor = UIColor.white            textFieldDOB.clipsToBounds = false
textFieldDOB.layer.shadowColor = UIColor.black.cgColor
textFieldDOB.layer.shadowOffset = CGSize.zero
textFieldDOB.layer.shadowOpacity = 0.3
textFieldDOB.layer.shadowRadius = 6
textFieldDOB.borderStyle = .none