Create imageView programmatically in Watch Kit
Asked Answered
W

2

6

i'm trying to create an image view for a watch app, i'm trying to create programmatically but the code used in a classic view controller doesn't work.

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
view.addSubview(imageView)

how i can do the same thing for watch kit? thanks

Woodwaxen answered 26/12, 2014 at 14:45 Comment(0)
U
14

You can't dynamically create views in WatchKit. You need to create your entire interface in a storyboard. You can have elements of your storyboard hidden and then programmatically unhide them.

In your storyboard you can use WKInterfaceImage to hold the image. You can set the image at runtime using setImage:.

Unctuous answered 27/12, 2014 at 16:15 Comment(0)
K
0

With SwiftUI, you can add a new image view to your view by using the ForEach and Image components.

struct ContentView: View {
    @State var images = [UIImage]()

    var body: some View {
        VStack {
            ForEach(images, id: \.self) { image in
                Image(uiImage: image)
            }
    
            Button("Add Image") {
                images.append(.init(imageLiteralResourceName: "image-name"))
            }
        }   
    }
}
Kevyn answered 13/12, 2022 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.