【发布时间】:2019-07-25 16:43:21
【问题描述】:
我有一个自定义的 tableView 单元,我不知道为什么我的 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。
【问题讨论】:
-
尝试给它一个高度限制。
-
父视图 (
self) 是否已添加到超级视图中?在调用您的setupNotificationBody()之前,超级和父 (self) 是否可见? -
@Kubee 不给它 topanchor 和 bottomanchor 与给它高度是一样的吗?
-
您是否使用了视图调试器?它报告了什么?
-
顺便说一句。这里有很多很多解决方案。但是,它们都取决于您的 UI。所以你会得到各种各样的答案
标签: swift uitableview uitextview