daredevil1234
daredevil1234

Reputation: 1445

UIView compatibility with SwiftUI using UIViewRepresentable not working

I have the following UIKit view that works well in UIKit:

    class ProfileAboutMeView: ProfileCardGenericView {
        var profileCardVariant: ProfileCardVariant = .meet
    
        var commentableButton: CommentableButton?
    
        private lazy var titleLabel: UILabel = {
            var titleLabel = UILabel()
            titleLabel.numberOfLines = 1
            return titleLabel
        }()
    
        var aboutLabel: UILabel = {
            var aboutLabel = UILabel()
            aboutLabel.numberOfLines = 0
            return aboutLabel
        }()
    
        init(profile: Profile, profileCardVariant: ProfileCardVariant = .meet, isCommentable: Bool = false, commentButtonDelegate: CommentableButtonDelegate? = nil) {
            super.init()
            self.profileCardVariant = profileCardVariant
            self.backgroundColor = .primaryLight
    
            if profileCardVariant == .spotlight || profileCardVariant == .meet {
                titleLabel.attributedText = TextStyle.header3.withColor(.primaryLightTextGray).withLineBreakMode(.byTruncatingMiddle).withAlignment(.left).attributedString("Profile.About.Title".localized)
                aboutLabel.attributedText = TextStyle.header4.withColor(.black).withLineBreakMode(.byWordWrapping).withAlignment(.left).attributedString(profile.aboutMe)
                self.setCorner()
            } else {
                titleLabel.attributedText = TextStyle.header3.withLineBreakMode(.byTruncatingMiddle).withAlignment(.left).attributedString("Profile.About.Title".localized)
                aboutLabel.attributedText = TextStyle.body.withColor(.black).withLineBreakMode(.byWordWrapping).withAlignment(.left).attributedString(profile.aboutMe)
            }
    
            self.addSubview(titleLabel)
            self.addSubview(aboutLabel)
            if isCommentable {
                self.commentableButton = CommentableButton(for: .about, delegate: commentButtonDelegate)
                self.addSubview(commentableButton)
            }
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func update(forProfile profile: Profile) {
            if let attText = aboutLabel.attributedText {
                let mutableText = NSMutableAttributedString(attributedString: attText)
                mutableText.mutableString.setString(profile.aboutMe)
                aboutLabel.attributedText = mutableText
            }
    
            self.setNeedsLayout()
        }
    
        override func layoutView() {
            print("layour profile about me view")
            titleLabel.snp.makeConstraints { make in
                make.top.leading.equalToSuperview().inset(16)
            }
    
            if let commentableButton, commentableButton.isDescendant(of: self) {
                aboutLabel.snp.makeConstraints { make in
                    make.leading.trailing.equalToSuperview().inset(16)
                    make.top.equalTo(titleLabel.snp.bottom).offset(4)
                    make.bottom.equalTo(commentableButton.snp.top).offset(-16)
                }
    
                self.commentableButton?.snp.makeConstraints { make in
                    make.leading.trailing.equalToSuperview().inset(20)
                    make.bottom.equalToSuperview().offset(self.profileCardVariant == .meet ? -16 : 0)
                    make.height.equalTo(44)
                }
            } else {
                aboutLabel.snp.makeConstraints { make in
                    make.leading.trailing.equalToSuperview().inset(16)
                    make.top.equalTo(titleLabel.snp.bottom).offset(4)
                    make.bottom.equalToSuperview().offset(self.profileCardVariant == .meet ? -16 : 0)
                }
            }
        }
    }

I am trying to make this view compatible with SwiftUI, so I have implemented UIViewRepresentable in the following way:

struct ProfileAboutMeViewRepresentable: UIViewRepresentable {
    var profile: Profile

    func makeUIView(context: Context) -> ProfileAboutMeView {
        let view = ProfileAboutMeView(profile: profile)
        view.layoutView()
        return view
    }

    func updateUIView(_ uiView: ProfileAboutMeView, context: Context) {
        uiView.update(forProfile: profile)
    }
} 

And I have tried using it in my SwiftUI view in the following way:

ProfileAboutMeViewRepresentable(profile: viewModel.otherUser)
   .frame(maxWidth: .infinity, alignment: .leading)
   .frame(maxHeight: .infinity, alignment: .bottom)

With no success. It simply does not render.

Anyone know what I may be doing wrong?

Upvotes: 0

Views: 78

Answers (0)

Related Questions