I am trying to achieve a layout of a text followed by an image (image height calculated based on aspect ratio) then followed by text and so on. The issue is that the stackview that I am adding the views into randomly squash the views sometimes the imageviews disappear some time the text, it doesn't have a consistent behaviour.
i tried it on both uitableview and uicolletion view and the result is the same. is the combination of the mentioned views considered as a best practice for such usecase or not ? and if not what might be the best practice for such thing ?
class MyStackyView: UIStackView {
// Main variables
weak var videoPlayerDelegate: AVPlayerViewDelegate?
private var avVideoPlayersVC: [AVPlayerViewController] = []
var content: [Content]! {
didSet {
contentCombined = Utility.shared.combineToNew(contents: content)
}
}
private var contentCombined: [Content] = [] {
didSet {
populatePostContent()
}
}
var contentViews: [UIView] = [] // Holds the views created
override init(frame: CGRect) {
super.init(frame: frame)
configureView()
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
print("DiaryPostView:: Deinitalized")
}
private func configureView() {
axis = .vertical
distribution = .fill
alignment = .fill
spacing = 0
}
}
// Extension to populate post content extension MyStackyView {
private func populatePostContent() {
for content in contentCombined {
if content.isMedia {
addMedia(content)
} else {
addText(content.text)
}
}
}
}
// Extension to add the required views extension MyStackyView {
private func addText(_ text: String?, place: MediaPlace = .center) {
let textView = generateDefaultTextView()
//let parsedText = HTMLParser.shared.parseHTMLToAttributed(string: text ?? "") // fix font issue
switch place {
case .center:
append(textView)
contentViews.append(textView)
}
textView.text = text
// لما استخدم ال parsedtext مرة النص بطلع مع الfont و مرة لا
}
private func addMedia(_ content: Content) {
let avPlayerVC = getAVPlayerViewController()
let mediaView = generateDefaultMediaView()
switch content.getRawPlace() {
case .center:
append(mediaView)
contentViews.append(mediaView)
addText(content.text)
NetworkManager().downloadMedia(content.img!, into: mediaView, avPlayerViewController: avPlayerVC) {
}
}
}
}
extension MyStackyView {
private func generateDefaultTextView() -> UILabel {
let textView = UILabel()
textView.backgroundColor = .clear
textView.numberOfLines = 0
textView.font = UIFont.customFont(.openSans, .regular, .title1, 17)
return textView
}
private func generateDefaultHorizontalStack() -> UIStackView {
let horizontalStack = UIStackView()
horizontalStack.axis = .horizontal
horizontalStack.distribution = .fill
horizontalStack.alignment = .fill
return horizontalStack
}
private func generateDefaultMediaView() -> MediaSliderView {
let mediaSliderView = MediaSliderView()
return mediaSliderView
}
private func getAVPlayerViewController() -> AVPlayerViewController? {
videoPlayerDelegate?.getAVPlayerVC?()
}
func deallocateAVPlayers() {
for player in avVideoPlayersVC {
player.removeFromParent()
}
avVideoPlayersVC.removeAll()
}
}
i initalize a variable of the class in my uitableviewcell and then add these constraints
contentView.addSubview(MyStackyView)
MyStackyView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8).isActive = true
MyStackyView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8).isActive = true
MyStackyView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16).isActive = true
MyStackyView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16).isActive = true
please if possible, i need some guidance about this issue.
thank you, appreciate the help