Hey there, I’m using kotlin to create a function t...
# ios
c
Hey there, I’m using kotlin to create a function that returns a view, but I’m having issues finding documentation about creating elements using the UIKit, for example, I already have buttons and labels, but I have no idea on how to add an image.. someone can point me in the right direction?
s
UIImageView
is used to hold an image. The documentation for UIKit is on Apple’s developer site. https://developer.apple.com/documentation/uikit?language=objc
c
Right now I have something like:
Copy code
@ObjCOutlet
    lateinit var logo: UIImageView

override fun viewDidLoad() {
        super.viewDidLoad()

logo = UIImageView().apply {
            image = UIImage.imageNamed("something")
        }

}
But I don’t think it’s right, because I don’t even know how to add a url for the image
s
You should use asset catalogs and reference the image by name that way. If you’re crossing over bundles/modules it gets trickier. By default it will look in the main bundle for your image. If you have images in another framework, you’ll have to get a reference to the bundle for that framework and pass that in as a parameter. Using asset catalogs also lets you support dark mode by specifying an alternate image in the catalog. Here’s an example in Swift.
Copy code
let image = UIImage(named: "imageName", in: Bundle(for: MyClass.self),                               compatibleWith: self.view.traitCollection)