我有一个自定义的 tableView Cell,我不明白为什么我的 UITextView 不会显示。 这是我的 UITextView:
fileprivate let notificationBody: UITextView = {
let text = UITextView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
text.translatesAutoresizingMaskIntoConstraints = false
text.backgroundColor = UIColor.appWhite
text.font = UIFont.preferredFont(forTextStyle: .headline)
text.isScrollEnabled = true
text.isEditable = false
text.text = "hjfhsdjfdsfhjksdfhksdjhkfsdjhfshjdfahsdjfdsjklfjsdkfjksdlfjkdsfjklsdlkjfsdjklfsdjklfsdjklfjkldsfjklsdfkjldskjfsdlkjfsdklfjlkdsjflksdajflkdsjflksdjflksdajflksdajflsdkjflsdkjflsdkjflsdkjflsdjflsdjflsdjfsdlkjfslkd"
text.textAlignment = NSTextAlignment.natural
return text
}()
这就是我的设置方式:
fileprivate func setupNotificationBody(){
addSubview(notificationBody)
NSLayoutConstraint.activate([
notificationBody.topAnchor.constraint(equalTo: notificationTitle.bottomAnchor, constant: 5),
notificationBody.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5),
notificationBody.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5),
notificationBody.bottomAnchor.constraint(equalTo: self.dismissNotificationButton.topAnchor, constant: -5)
])
}
当然,我在 Init 中调用了 setupNotificationBody。 我在这个单元格中还有另一个按钮和一个正确显示的 uilabel。
最佳答案
看起来您需要单元格具有动态高度。在这种情况下,您应该将 UITextView
中的属性 isScrollEnabled
设置为 false,以便 UITextView
将能够计算其自身的高度。
另外,请记住在 UITableViewDelegate
中将单元格高度设置为 UITableView.automaticDimension
。
关于swift - 为什么UITextView不会出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57206722/