UITextView 文本有时不会填满 TableViewCell 中的所有 space
UITextView text sometimes not filling all space in TableViewCell
我正在从 Twitter API 中提取数据,并将我的 table 单元格设置为包含 UITextView 以显示推文。我遇到一个问题,有时文本在换行前只占用可用 space 的一小部分,如下图所示:
但是,如果我返回点击导航控制器,然后重新加载数据,它就会正常工作:
谁能解释为什么会这样?以下是我的 table 视图的设置方式:
func numberOfSections(in tableView: UITableView) -> Int {
if let tweets = timeline?.tweets {
return 1
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if timeline == nil {
return 0
} else {
if let _timeline = timeline {
return _timeline.tweets!.count
}
}
return 1
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.TweetTableViewCell, for: indexPath) as! TweetTableViewCell
if let _timeline = timeline {
if let tweet = _timeline.tweets?[indexPath.row] {
cell.tweet = tweet
cell.tweetTextView.translatesAutoresizingMaskIntoConstraints = true
cell.tweetTextView.sizeToFit()
cell.tweetTextView.isScrollEnabled = false
}
}
return cell
}
这是我的 TableViewCell:
class TweetTableViewCell: UITableViewCell {
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var profileNameLabel: UILabel!
@IBOutlet weak var profileTwitterUsername: UILabel!
@IBOutlet weak var tweetTextView: UITextView!
@IBOutlet weak var tweetDateLabel: UILabel!
@IBOutlet weak var tweetTimeLabel: UILabel!
@IBOutlet weak var mediaImageView: UIImageView!
@IBOutlet weak var likesCountLabel: UILabel!
var tweet: Tweet? {
didSet {
self.updateUI()
}
}
func updateUI() {
if let tweet = tweet {
mediaImageView.isHidden = true
let profileImageOriginalSizeUrl = tweet.user.profile_image_url_https.replacingOccurrences(of: "_normal", with: "")
if let profileImageOriginalSizeUrl = URL(string: profileImageOriginalSizeUrl) {
profileImageView.sd_setImage(with: profileImageOriginalSizeUrl, completed: nil) // Get image from cache
}
roundProfileImage()
profileNameLabel.text = tweet.user.name
profileTwitterUsername.text = "@\(tweet.user.screen_name)"
tweetTextView.textContainerInset = UIEdgeInsets.zero
tweetTextView.text = tweet.full_text
tweetDateLabel.text = truncateDate(withDate: tweet.created_at)
tweetTimeLabel.text = truncateTime(withDate: tweet.created_at)
}
if let media = tweet?.entities?.media?[0].media_url_https {
mediaImageView.isHidden = false
if let mediaImageUrl = URL(string: media) {
mediaImageView.sd_setImage(with: mediaImageUrl, completed: nil)
}
}
}
此外,如果有帮助,这是我的情节提要,其中包含包含 UITextView 和 UIImageView 以及约束的堆栈视图:
当应用程序 运行:
时,控制台会显示其中一些警告
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a6a0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.height == 95.5 (active)>",
"<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-08-03 04:57:43.373999+0100 StarChallenge[22459:10425072] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a5b0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.minX == 0 (active, names: '|':UIStackView:0x7b4c00045c80 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a600 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.width == 348.5 (active)>",
"<NSLayoutConstraint:0x7b1400046e60 H:[UITextView:0x7b740006d600'I'm back with a brand new...']-(30)-| (active, names: '|':UIStackView:0x7b4c00045c80 )>",
"<NSLayoutConstraint:0x7b1400047040 H:|-(10)-[UIStackView:0x7b4c00045c80] (active, names: '|':UITableViewCellContentView:0x7b4c00045ac0 )>",
"<NSLayoutConstraint:0x7b1400047090 H:[UIStackView:0x7b4c00045c80]-(10)-| (active, names: '|':UITableViewCellContentView:0x7b4c00045ac0 )>",
"<NSLayoutConstraint:0x7b140004ae70 'fittingSizeHTarget' UITableViewCellContentView:0x7b4c00045ac0.width == 375 (active)>"
)
在自动布局完成设置之前,您的单元已完成将数据加载到插座中。
Override layoutSubviews() {
Super.layoutSubviews
// Call your setup method
}
这是在自动布局完成后调用的。
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a6a0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.height == 95.5 (active)>",
"<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>"
你设置TextView的constraint height = 100,在调整标记后,constrain height为95.5,与之前的值冲突。
- 您可以尝试
remove
或将您的TextView的约束高度的优先级设置为低(250)。
或使用UILabel
代替,你不必为标签设置约束高度,也不必使用AutoresizingMask
& sizeToFit()
。
我正在从 Twitter API 中提取数据,并将我的 table 单元格设置为包含 UITextView 以显示推文。我遇到一个问题,有时文本在换行前只占用可用 space 的一小部分,如下图所示:
但是,如果我返回点击导航控制器,然后重新加载数据,它就会正常工作:
谁能解释为什么会这样?以下是我的 table 视图的设置方式:
func numberOfSections(in tableView: UITableView) -> Int {
if let tweets = timeline?.tweets {
return 1
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if timeline == nil {
return 0
} else {
if let _timeline = timeline {
return _timeline.tweets!.count
}
}
return 1
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.TweetTableViewCell, for: indexPath) as! TweetTableViewCell
if let _timeline = timeline {
if let tweet = _timeline.tweets?[indexPath.row] {
cell.tweet = tweet
cell.tweetTextView.translatesAutoresizingMaskIntoConstraints = true
cell.tweetTextView.sizeToFit()
cell.tweetTextView.isScrollEnabled = false
}
}
return cell
}
这是我的 TableViewCell:
class TweetTableViewCell: UITableViewCell {
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var profileNameLabel: UILabel!
@IBOutlet weak var profileTwitterUsername: UILabel!
@IBOutlet weak var tweetTextView: UITextView!
@IBOutlet weak var tweetDateLabel: UILabel!
@IBOutlet weak var tweetTimeLabel: UILabel!
@IBOutlet weak var mediaImageView: UIImageView!
@IBOutlet weak var likesCountLabel: UILabel!
var tweet: Tweet? {
didSet {
self.updateUI()
}
}
func updateUI() {
if let tweet = tweet {
mediaImageView.isHidden = true
let profileImageOriginalSizeUrl = tweet.user.profile_image_url_https.replacingOccurrences(of: "_normal", with: "")
if let profileImageOriginalSizeUrl = URL(string: profileImageOriginalSizeUrl) {
profileImageView.sd_setImage(with: profileImageOriginalSizeUrl, completed: nil) // Get image from cache
}
roundProfileImage()
profileNameLabel.text = tweet.user.name
profileTwitterUsername.text = "@\(tweet.user.screen_name)"
tweetTextView.textContainerInset = UIEdgeInsets.zero
tweetTextView.text = tweet.full_text
tweetDateLabel.text = truncateDate(withDate: tweet.created_at)
tweetTimeLabel.text = truncateTime(withDate: tweet.created_at)
}
if let media = tweet?.entities?.media?[0].media_url_https {
mediaImageView.isHidden = false
if let mediaImageUrl = URL(string: media) {
mediaImageView.sd_setImage(with: mediaImageUrl, completed: nil)
}
}
}
此外,如果有帮助,这是我的情节提要,其中包含包含 UITextView 和 UIImageView 以及约束的堆栈视图:
当应用程序 运行:
时,控制台会显示其中一些警告Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a6a0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.height == 95.5 (active)>",
"<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-08-03 04:57:43.373999+0100 StarChallenge[22459:10425072] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a5b0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.minX == 0 (active, names: '|':UIStackView:0x7b4c00045c80 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a600 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.width == 348.5 (active)>",
"<NSLayoutConstraint:0x7b1400046e60 H:[UITextView:0x7b740006d600'I'm back with a brand new...']-(30)-| (active, names: '|':UIStackView:0x7b4c00045c80 )>",
"<NSLayoutConstraint:0x7b1400047040 H:|-(10)-[UIStackView:0x7b4c00045c80] (active, names: '|':UITableViewCellContentView:0x7b4c00045ac0 )>",
"<NSLayoutConstraint:0x7b1400047090 H:[UIStackView:0x7b4c00045c80]-(10)-| (active, names: '|':UITableViewCellContentView:0x7b4c00045ac0 )>",
"<NSLayoutConstraint:0x7b140004ae70 'fittingSizeHTarget' UITableViewCellContentView:0x7b4c00045ac0.width == 375 (active)>"
)
在自动布局完成设置之前,您的单元已完成将数据加载到插座中。
Override layoutSubviews() {
Super.layoutSubviews
// Call your setup method
}
这是在自动布局完成后调用的。
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a6a0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.height == 95.5 (active)>",
"<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>"
你设置TextView的constraint height = 100,在调整标记后,constrain height为95.5,与之前的值冲突。
- 您可以尝试
remove
或将您的TextView的约束高度的优先级设置为低(250)。
或使用UILabel
代替,你不必为标签设置约束高度,也不必使用AutoresizingMask
& sizeToFit()
。