Search code examples
swiftuiscrollview

Update UIImageView with didset function


I created a page structure using Scrollview. I update the paragraphText and HeaderText with didset, but I can't update the image. What do I need to do to update the image? Everything works except the image right now.

class PageView: UIView {

    var headerText: String = "" {
        didSet {
            headerTextField.text = headerText
        }
    }

    var paragraphText: String = "" {
        didSet {
            paragraphTextView.text = paragraphText
        }
    }

    private var cellImage = UIImageView()
     var imagee: UIImage? {
        didSet {
           cellImage.image = imagee
        }
    }

class PageController: UIViewController {

    let placeHolderText = "........"

    let placeHolderText1 = "......"
    var cellImage = UIImage(named: "asdasd")
    var cellImageLogin = UIImage(named: "asdasdff")

 let pageView1 = PageView(headerText: "123123", paragraphText: placeHolderText, backgroundColor: .red, image: cellImage! )
        views.append(pageView1)
 let pageView2 = PageView(headerText: "123213123", paragraphText: placeHolderText1, backgroundColor: .orange, image: cellImageLogin!)
        views.append(pageView2)

Solution

  • As your cellImage don't have frame it won't be visible in your PageView

    You need to add frame with for example

    cellImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
    

    in your setup method before self.addSubview(cellImage)

    Also you need to apply the correct constraints as per your requirement.