Swift: как заставить тень работать на UITextView из ViewDidLoad? - PullRequest
0 голосов
/ 28 мая 2020

Я не могу получить тень на UITextView, который находится внутри UIImageView.

Я пробовал много возможных способов. Ограничение заключается в том, что я не могу переопределить layoutMethods, поскольку они по какой-то причине недоступны. Итак, для демонстрации я поместил тот же код в viewDidLoad для отладки, но не смог его понять. Пожалуйста, объясните, что я здесь делаю не так.

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .black
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "guitar")
        imageView.layer.cornerRadius = 15
        imageView.clipsToBounds = true

        view.addSubview(imageView)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.heightAnchor.constraint(equalToConstant: 240).isActive = true
        imageView.widthAnchor.constraint(equalToConstant: 240).isActive = true
        imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

        let textView = UITextView(frame: CGRect.zero)
        textView.text = "This is a Sample text. This is a Sample text. This is a Sample text. This is a Sample text. This is a Sample text. "
        textView.isEditable = false
        textView.textAlignment = .left
        textView.isSelectable = false
        textView.backgroundColor = .systemBlue
        textView.textColor = UIColor.white
        textView.sizeToFit()
        textView.isScrollEnabled = false
        textView.textContainerInset = UIEdgeInsets(top: 6, left: 7, bottom: 6, right: 7)
        textView.clipsToBounds = false
        textView.layer.shadowColor = UIColor.gray.cgColor
        textView.layer.shadowRadius = 10
        textView.layer.opacity = 1
        textView.layer.shadowOffset = .zero
        textView.layer.shadowPath = UIBezierPath(rect: textView.bounds).cgPath
        textView.layer.masksToBounds = false

        imageView.addSubview(textView)
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.leftAnchor.constraint(equalTo: imageView.leftAnchor).isActive = true
        textView.bottomAnchor.constraint(equalTo: imageView.bottomAnchor).isActive = true
        textView.rightAnchor.constraint(equalTo: imageView.rightAnchor).isActive = true
    }

1 Ответ

0 голосов
/ 28 мая 2020

Вы устанавливаете СЛОЙ непрозрачность на 1:

textView.layer.opacity = 1

, но вам нужно установить shadowOpacity * 1011 слоя. *:

//textView.layer.opacity = 1

textView.layer.shadowOpacity = 1

Это должно решить проблему.

Между прочим, вы получите тот же результат с .shadowPath или без него (поскольку путь тени по умолчанию - границы / прямоугольник вида).

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...