DEV Community

Romain Sickenberg
Romain Sickenberg

Posted on

5 2

Are you using Lazy for your UIView Components ?

Hey folks,

First time I wrote on Dev!

I have a simple question for you my Swifters and iOS developers.

Do you use Lazy implementation on your conception?

// Without Lazy
var imageView: UIImageView = {
     let imageView = UIImageView(forAutoLayout: ())
     imageView.image = Asset.mapIconBold.image
     imageView.frame = CGRect(x: 0, y: 0, width: 10, height: 10)

     return imageView
}()

// With Lazy
lazy var reloadNavBarButton: BaseButton = {
    let button = BaseButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
    button.configureForAutoLayout()
    button.setImage(Asset.reload.image, for: .normal)
    button.imageView?.setImageColor(color: .white)
    button.addTarget(self, action: #selector(reloadApplication), for: .touchUpInside)
    button.isHidden = true
    button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 15, right: 10)

    return button
}()

Enter fullscreen mode Exit fullscreen mode

See you !

Sentry blog image

The countdown to March 31 is on.

Make the switch from app center suck less with Sentry.

Read more

Top comments (2)

Collapse
 
acodeguy profile image
acodeguy

I almost always make my UI components lazy. I also make them private 😜

Collapse
 
rsickenberg profile image
Romain Sickenberg

Well, I used to make them private but sometimes I modify them through the ViewController, in my case, Protected would be ideal but it does not exist in Swift.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more