https://kotlinlang.org logo
#ios
Title
# ios
c

Christian Sousa

03/12/2020, 3:34 PM
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

Sam

03/12/2020, 3:51 PM
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

Christian Sousa

03/12/2020, 4:04 PM
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

Sam

03/12/2020, 4:53 PM
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)
6 Views