gpt4 book ai didi

swift - 更改 iOS 中 TableviewCell 的左右填充

转载 作者:行者123 更新时间:2023-11-30 11:02:43 25 4
gpt4 key购买 nike

我正在尝试制作一个气泡UITableviewCell。单元格有一个 UILabel,所有 edges 都固定到单元格 ContentView 的边缘,并且 UITableView 使用 AutomaticHeight (基于Label.text)我已将CellBackgroundView图像设置为下面的函数

func changeImage(_ name: String) -> UIImage? {
guard let image = UIImage(named: name) else { return nil }
return image.resizableImage(withCapInsets: UIEdgeInsets(top: 17, left: 30, bottom: 17, right: 30), resizingMode: .stretch).withRenderingMode(.alwaysTemplate)
}

上面的函数在cellforRowAtIndexPath中调用:

cell.backgroundView = UIImageView(image: changeImage("right_bubble"))

我的背景图像看起来像气泡:

enter image description here

问题是我无法更改 Cell 的左侧或右侧 padding。有没有办法更改填充(可能是左或右)(以区分发送者和接收者)或需要从 UITableView 更改为 UICollectionView

编辑

我提出了一个在自定义单元子类中动态更改约束的解决方案。下面是子类的实现

import UIKit

enum Direction {
case left
case right
}

class MessageCell: UITableViewCell {

@IBOutlet weak var backgroundImageView: UIImageView!

@IBOutlet weak var messageText: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func prepareForReuse() {
self.layoutIfNeeded()
}

func configureCell(with message: Message, direction: Direction ) {
self.messageText?.text = message.text

switch direction {
case .left:
backgroundImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10).isActive = true
contentView.trailingAnchor.constraint(greaterThanOrEqualTo: backgroundImageView.trailingAnchor, constant: 50).isActive = true
self.backgroundImageView.image = UIImage.changeImage("left_bubble")
self.backgroundImageView.tintColor = .lightGray
case .right:
backgroundImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10).isActive = true
backgroundImageView.leadingAnchor.constraint(greaterThanOrEqualTo: contentView.leadingAnchor, constant: 50).isActive = true
self.backgroundImageView.image = UIImage.changeImage("right_bubble")
self.backgroundImageView.tintColor = .blue
}
self.layoutIfNeeded()
}

}

但是当行数超过屏幕高度并需要滚动时,自动布局会遇到问题。以下是错误:

2018-11-03 19:53:36.478275+0800 VicGithubDM[2352:34611] [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.
(
"<NSLayoutConstraint:0x6000006e9090 V:|-(10)-[UILabel:0x7fe37dd3bb70'still have some bugs stil...'] (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>",
"<NSLayoutConstraint:0x6000006ea620 V:[UILabel:0x7fe37dd3bb70'still have some bugs stil...']-(10)-| (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>",
"<NSLayoutConstraint:0x6000006e1d10 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe37dd3b750.height == 1.19209e-07 (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000006ea620 V:[UILabel:0x7fe37dd3bb70'still have some bugs stil...']-(10)-| (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>

最佳答案

我建议创建一个自定义单元格并根据类型更改约束值。

关于swift - 更改 iOS 中 TableviewCell 的左右填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53122884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com