Как получить UITextView для привязки справа от UICollectionViewCell? - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть UITextView, который я хочу привязать справа от моего UICollectionViewCell. Я применяю следующее ограничение: textView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true Но оно НЕ привязывается полностью вправо. Затем я применяю следующее ограничение, но на этот раз: textView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true, и оно ДЕЙСТВИТЕЛЬНО привязывается полностью влево. Почему это может быть?

https://imgur.com/a/uykWLw5

Вот мой ChatMessageCell.

import UIKit

class ChatMessageCell: UICollectionViewCell {

    let textView: UITextView = {
        let tv = UITextView()
        tv.text = "Some Text"
        tv.font = UIFont.systemFont(ofSize: 16)
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(textView)

        textView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        textView.widthAnchor.constraint(equalToConstant: 200).isActive = true
        textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

1 Ответ

0 голосов
/ 23 апреля 2020

Ваш textView будет выровнен по левому краю. Если вы хотите, чтобы текстовое представление оставалось с шириной 200 и отображалось справа, вам нужно добавить: textView.textAlignment = .right

Кроме того, я полагаю, что лучше всего использовать начальные и конечные ограничения

...