Как сделать Uiview подготовить размер его содержимого?IOS

Программируем под IOS
Ответить
Anonymous
 Как сделать Uiview подготовить размер его содержимого?

Сообщение Anonymous »

У меня есть следующий код < /p>
func setupUI() {
self.view.backgroundColor = .white

let titleLbl = UILabel()
titleLbl.text = book.title
titleLbl.textColor = .black
titleLbl.textAlignment = .center
titleLbl.numberOfLines = 0
titleLbl.lineBreakMode = .byWordWrapping
titleLbl.font = UIFont(name: "Inter-Regular", size: 16);

view.addSubview(titleLbl)

// Using Auto Layout
titleLbl.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
titleLbl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
titleLbl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
titleLbl.heightAnchor.constraint(equalToConstant: 20)
])

innerView = UIView()
innerView.backgroundColor = UIColor(red: 0.92, green: 0.96, blue: 0.99, alpha: 1.00)
innerView.layer.cornerRadius = 12
innerView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(innerView)

NSLayoutConstraint.activate([
innerView.topAnchor.constraint(equalTo: titleLbl.bottomAnchor, constant: 40),
innerView.heightAnchor.constraint(equalToConstant: 300),
innerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20),
innerView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20)
])

let backBtn = UIButton(frame: CGRect(x: self.view.frame.width - 60, y: 55, width: 40, height: 40))
backBtn.setImage(UIImage.init(named: "close-circle"), for: .normal)
backBtn.backgroundColor = UIColor.clear
backBtn.addTarget(self, action: #selector(goback), for: .touchUpInside)
self.view.addSubview(backBtn)

let quote = self.book.quotes![self.currentIndex]

let bodyLbl = UILabel()
bodyLbl.text = quote
bodyLbl.textColor = .black
bodyLbl.textAlignment = .center
bodyLbl.numberOfLines = 0
bodyLbl.lineBreakMode = .byWordWrapping
bodyLbl.font = UIFont(name: "Inter-Regular", size: 16);

innerView.addSubview(bodyLbl)

// Using Auto Layout
bodyLbl.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
bodyLbl.leadingAnchor.constraint(equalTo: innerView.safeAreaLayoutGuide.leadingAnchor, constant: 10),
bodyLbl.topAnchor.constraint(equalTo: innerView.safeAreaLayoutGuide.topAnchor, constant: 20),
bodyLbl.trailingAnchor.constraint(equalTo: innerView.safeAreaLayoutGuide.trailingAnchor, constant: -10)
])
bodyLbl.sizeToFit()

let shareButton = UIButton()
shareButton.setTitle("Share", for: .normal)
shareButton.setTitleColor(.white, for: .normal)
shareButton.backgroundColor = .black
shareButton.addTarget(self, action: #selector(self.share), for: .touchUpInside)
shareButton.layer.cornerRadius = 12
shareButton.clipsToBounds = true
shareButton.isUserInteractionEnabled = true
innerView.addSubview(shareButton)

shareButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
shareButton.centerXAnchor.constraint(equalTo: innerView.centerXAnchor),
shareButton.topAnchor.constraint(equalTo: bodyLbl.bottomAnchor, constant: 40),
shareButton.heightAnchor.constraint(equalToConstant: 30),
shareButton.widthAnchor.constraint(equalToConstant: 150)
])

let count = self.book.quotes!.count
if ((self.currentIndex + 1) > count) {
self.nextBtn.isHidden = true
} else {
self.nextBtn.isHidden = false
}

innerView.setNeedsLayout()
innerView.layoutIfNeeded()

let newSize = innerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
innerView.frame.size = newSize
innerView.sizeToFit()
}
< /code>
Как я могу сделать innerview подходить к размеру его содержимого? Я установил высоту на 300, но мне нужно, чтобы она была впоследствии, чтобы соответствовать размеру метки и кнопки обмена. Кто -нибудь может помочь? Что я делаю не так? Я попробовал Sizetofit, но это не работает.

Подробнее здесь: https://stackoverflow.com/questions/797 ... s-contents
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»