0

I declare in class ViewController an array map_sector_UIImage of UIImage.

But in viewDidLoad() I get error when appending 10 UIImage's to the array and error when I view.addSubview .

Each UIImage is to be a colored square

I get build errors (see below).

    NOTE:   let App.total_map_sectors: Int = 10
    

class ViewController: UIViewController 
{
            static var map_sector_UIImage: [UIImage] = [] 

override func viewDidLoad()     
{
App.init_class() 
            // Allocate array of sector UIImage:
            for map_sector_index in 0...App.total_map_sectors-1
            {
                ViewController.map_sector_UIImage[ map_sector_index ] += 
 E R R O R   Referencing operator function '+=' on 'FloatingPoint' requires that 'UIImage' conform to 'FloatingPoint'
 {  
                    let theImage = UIImage()
                    theImage.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
                    return theImage
                }()
            }
            super.viewDidLoad()

            for map_sector_index in 0...App.total_map_sectors-1
            {
                view.addSubview( ViewController.map_sector_UIImage[ map_sector_index ] )
E R R O R    Referencing operator function '+=' on 'FloatingPoint' requires that 'UIImage' conform to 'FloatingPoint'
            }


           for sector_index in 0...App.total_map_sectors - 1
           {
                ViewController.map_sector_UIImage[ sector_index ] =
                    // swift 4.0
                    UIColor.black.image(CGSize( width: App.sector_width_pix, height: App.sector_height_pix ))
                    // swift 3.0 = UIImage(color: .blackColor(), size: CGSize(width: App.sector_width_pix, height: App.sector_height_pix ))
                // https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
                    // eg. let redImage200x200 = UIImage(color: .redColor(), size: CGSize(width: 200, height: 200))
           }
}



        
    // Swift 4.0
    // From:
                // https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
    extension UIColor {
    func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
        return UIGraphicsImageRenderer(size: size).image { rendererContext in
            self.setFill()
            rendererContext.fill(CGRect(origin: .zero, size: size))
        }
    }
 }
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • `translatesAutoresizingMaskIntoConstraints` isn't available on `UIImage`. It's only available on `UIView` -- maybe you're meaning to use `UIImageView`? Same issue later -- you can't use `addSubview` with `UIImage` as the parameter. – jnpdx Jul 07 '21 at 19:48
  • And a solid image in an image view is a silly way to make a simple colored rectangle. Just a plain UIView with a background color will do. – matt Jul 07 '21 at 20:17
  • Thanks for advice, matt. I will try that immediately. – Doug Null Jul 08 '21 at 16:40

0 Answers0