Swift 5 Migration has Changed Circular Images

I have a table view controller with many table view sections. In each table view section, there is a table view cell. In each table view cell, there is a content view. In each content view, there is some text and an imageView. The image view is a round profile picture with border. It has always worked. This is the code for each image view:


self.imageView1.layer.cornerRadius = self.imageView1.frame.size.width / 2;

self.imageView1.clipsToBounds = true;

self.imageView1.layer.borderWidth = 2.0

self.imageView1.layer.borderColor = UIColor.white.cgColor

I just migrated to Swift 5, and the circles are now square. No coding changes have been made whatsoever. In fact, when I migrated, the changes that it showed that it would make shouldn't have interfered at all with the roundness of the image.

I am at an utter loss as to how to correct it.

Replies

I tested, with Swift5 and it works OK, exactly as with Swift 4.2


I included that code in viewDidAppear.


Where did you put the code ?

My code is and always has been in the viewDidLoad. It worked before, but no longer works.

I moved the code from viewDidLoad to viewDidAppear and it changed the first half of the images back to circles but left the last half square. I just don't understand.

Here is the viewDidAppear that works


    override func viewDidAppear(_ animated: Bool) {
       
        externalPicture.image = UIImage (named: externalArray [1])     // I keep images in array, but no importance
        externalPicture.layer.cornerRadius = externalPicture.frame.size.width / 2;
        externalPicture.clipsToBounds = true;
        externalPicture.layer.borderWidth = 2.0
        externalPicture.layer.borderColor = UIColor.red.cgColor

    }

I added a second image, works OK as well

        externalPicture2.image = UIImage (named: externalArray [0])
        externalPicture2.layer.cornerRadius = externalPicture2.frame.size.width / 2;
        externalPicture2.clipsToBounds = true;
        externalPicture2.layer.borderWidth = 2.0
        externalPicture2.layer.borderColor = UIColor.blue.cgColor


What do you man it changed the first half of the images back to circles but left the last half square


Are you speaking of 2 halves of an image ? Which galf OK and which non OK ? What do you get exactly, can you describe ?

Or half of a series of images ?

I use the code repeadedly for many images in a row. So, only half of them are changing to circles now. Example:


self.imageView1.layer.cornerRadius = self.imageView1.frame.size.width / 2;

self.imageView1.clipsToBounds = true;

self.imageView1.layer.borderWidth = 2.0

self.imageView1.layer.borderColor = UIColor.white.cgColor


self.imageView2.layer.cornerRadius = self.imageView2.frame.size.width / 2;

self.imageView2.clipsToBounds = true;

self.imageView2.layer.borderWidth = 2.0

self.imageView2.layer.borderColor = UIColor.white.cgColor


self.imageView3.layer.cornerRadius = self.imageView3.frame.size.width / 2;

self.imageView3.clipsToBounds = true;

self.imageView3.layer.borderWidth = 2.0

self.imageView3.layer.borderColor = UIColor.white.cgColor


self.imageView4.layer.cornerRadius = self.imageView4.frame.size.width / 2;

self.imageView4.clipsToBounds = true;

self.imageView4.layer.borderWidth = 2.0

self.imageView4.layer.borderColor = UIColor.white.cgColor


When I put all of this code in viewDidAppear as you suggest out of viewDidLoad, only imageView1 and imageView2 changed to circles...imageView3 and imageView4 stayed square. They are all square in viewDidLoad, but in the previous versions of swift, all images were circles as they were supposed to be.

You say each image view is placed in a table view cell, how can you access it in vewDidLoad() or viewDidAppear() ?

UITableViewCell does not have such methods. Please show all relevant codes.

Each table view cell has a content view. Inside the content view is the imageview where the profile pictire is. Each imageView is connected to a weakIBOutlet on the tableviewcontroller.swift file. Further down on that file is the code:


self.imageView1.layer.cornerRadius = self.imageView1.frame.size.width / 2;

self.imageView1.clipsToBounds = true;

self.imageView1.layer.borderWidth = 2.0

self.imageView1.layer.borderColor = UIColor.white.cgColor


for each image.

You have no need to show the same code again. Please show your hidden code.

I don't understand...what "hidden code?"

For example, whole lines of the `viewDidLoad()` method, all the `UITableViewDataSource` methods...

Have you shown those?

This is the swift code that the tableviewcontroller on the storyboard is attached to:



customTableViewController.swift


import UIKit


class customTableViewController: UITableViewController {
  
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    @IBOutlet weak var imageView3: UIImageView!
    @IBOutlet weak var imageView4: UIImageView!
    @IBOutlet weak var imageView5: UIImageView!
    @IBOutlet weak var imageView6: UIImageView!
    @IBOutlet weak var imageView7: UIImageView!
    @IBOutlet weak var imageView8: UIImageView!
    @IBOutlet weak var imageView9: UIImageView!
    @IBOutlet weak var imageView10: UIImageView!
    @IBOutlet weak var imageView11: UIImageView!
    @IBOutlet weak var imageView12: UIImageView!
    @IBOutlet weak var imageView13: UIImageView!
    @IBOutlet weak var imageView14: UIImageView!
    @IBOutlet weak var imageView15: UIImageView!
    @IBOutlet weak var imageView16: UIImageView!
    @IBOutlet weak var imageView17: UIImageView!
    @IBOutlet weak var imageView18: UIImageView!
    @IBOutlet weak var imageView19: UIImageView!
    @IBOutlet weak var imageView20: UIImageView!
    @IBOutlet weak var imageView21: UIImageView!
    @IBOutlet weak var imageView22: UIImageView!
    @IBOutlet weak var imageView23: UIImageView!
    @IBOutlet weak var imageView24: UIImageView!
    @IBOutlet weak var imageView25: UIImageView!
    @IBOutlet weak var imageView26: UIImageView!
    @IBOutlet weak var imageView27: UIImageView!
    @IBOutlet weak var imageView28: UIImageView!
    @IBOutlet weak var imageView29: UIImageView!
    @IBOutlet weak var imageView30: UIImageView!
    @IBOutlet weak var imageView31: UIImageView!
    @IBOutlet weak var imageView32: UIImageView!
    @IBOutlet weak var imageView33: UIImageView!
    @IBOutlet weak var imageView34: UIImageView!
    @IBOutlet weak var imageView35: UIImageView!
    @IBOutlet weak var imageView36: UIImageView!
    @IBOutlet weak var imageView37: UIImageView!
    @IBOutlet weak var imageView38: UIImageView!
    @IBOutlet weak var imageView39: UIImageView!
    @IBOutlet weak var imageView40: UIImageView!
    @IBOutlet weak var imageView41: UIImageView!
    @IBOutlet weak var imageView42: UIImageView!
    @IBOutlet weak var imageView43: UIImageView!
    @IBOutlet weak var imageView44: UIImageView!
    @IBOutlet weak var imageView45: UIImageView!
  
  
  
  
 
  
    override func viewDidLoad() {
   

      
        self.imageView1.layer.cornerRadius = self.imageView1.frame.size.width / 2;
        self.imageView1.clipsToBounds = true;
        self.imageView1.layer.borderWidth = 2.0
        self.imageView1.layer.borderColor = UIColor.white.cgColor
      
        self.imageView2.layer.cornerRadius = self.imageView2.frame.size.width / 2;
        self.imageView2.clipsToBounds = true;
        self.imageView2.layer.borderWidth = 2.0
        self.imageView2.layer.borderColor = UIColor.white.cgColor
      
        self.imageView3.layer.cornerRadius = self.imageView3.frame.size.width / 2;
        self.imageView3.clipsToBounds = true;
        self.imageView3.layer.borderWidth = 2.0
        self.imageView3.layer.borderColor = UIColor.white.cgColor
      
        self.imageView4.layer.cornerRadius = self.imageView4.frame.size.width / 2;
        self.imageView4.clipsToBounds = true;
        self.imageView4.layer.borderWidth = 2.0
        self.imageView4.layer.borderColor = UIColor.white.cgColor
      
        self.imageView5.layer.cornerRadius = self.imageView5.frame.size.width / 2;
        self.imageView5.clipsToBounds = true;
        self.imageView5.layer.borderWidth = 2.0
        self.imageView5.layer.borderColor = UIColor.white.cgColor
      
        self.imageView6.layer.cornerRadius = self.imageView6.frame.size.width / 2;
        self.imageView6.clipsToBounds = true;
        self.imageView6.layer.borderWidth = 2.0
        self.imageView6.layer.borderColor = UIColor.white.cgColor
      
        self.imageView7.layer.cornerRadius = self.imageView7.frame.size.width / 2;
        self.imageView7.clipsToBounds = true;
        self.imageView7.layer.borderWidth = 2.0
        self.imageView7.layer.borderColor = UIColor.white.cgColor
      
        self.imageView8.layer.cornerRadius = self.imageView8.frame.size.width / 2;
        self.imageView8.clipsToBounds = true;
        self.imageView8.layer.borderWidth = 2.0
        self.imageView8.layer.borderColor = UIColor.white.cgColor
      
        self.imageView9.layer.cornerRadius = self.imageView9.frame.size.width / 2;
        self.imageView9.clipsToBounds = true;
        self.imageView9.layer.borderWidth = 2.0
        self.imageView9.layer.borderColor = UIColor.white.cgColor
      
        self.imageView10.layer.cornerRadius = self.imageView10.frame.size.width / 2;
        self.imageView10.clipsToBounds = true;
        self.imageView10.layer.borderWidth = 2.0
        self.imageView10.layer.borderColor = UIColor.white.cgColor
      
        self.imageView11.layer.cornerRadius = self.imageView11.frame.size.width / 2;
        self.imageView11.clipsToBounds = true;
        self.imageView11.layer.borderWidth = 2.0
        self.imageView11.layer.borderColor = UIColor.white.cgColor
      
        self.imageView12.layer.cornerRadius = self.imageView12.frame.size.width / 2;
        self.imageView12.clipsToBounds = true;
        self.imageView12.layer.borderWidth = 2.0
        self.imageView12.layer.borderColor = UIColor.white.cgColor
      
        self.imageView13.layer.cornerRadius = self.imageView13.frame.size.width / 2;
        self.imageView13.clipsToBounds = true;
        self.imageView13.layer.borderWidth = 2.0
        self.imageView13.layer.borderColor = UIColor.white.cgColor
      
        self.imageView14.layer.cornerRadius = self.imageView14.frame.size.width / 2;
        self.imageView14.clipsToBounds = true;
        self.imageView14.layer.borderWidth = 2.0
        self.imageView14.layer.borderColor = UIColor.white.cgColor
      
        self.imageView15.layer.cornerRadius = self.imageView15.frame.size.width / 2;
        self.imageView15.clipsToBounds = true;
        self.imageView15.layer.borderWidth = 2.0
        self.imageView15.layer.borderColor = UIColor.white.cgColor
      
        self.imageView16.layer.cornerRadius = self.imageView16.frame.size.width / 2;
        self.imageView16.clipsToBounds = true;
        self.imageView16.layer.borderWidth = 2.0
        self.imageView16.layer.borderColor = UIColor.white.cgColor
      
        self.imageView17.layer.cornerRadius = self.imageView17.frame.size.width / 2;
        self.imageView17.clipsToBounds = true;
        self.imageView17.layer.borderWidth = 2.0
        self.imageView17.layer.borderColor = UIColor.white.cgColor
      
        self.imageView18.layer.cornerRadius = self.imageView18.frame.size.width / 2;
        self.imageView18.clipsToBounds = true;
        self.imageView18.layer.borderWidth = 2.0
        self.imageView18.layer.borderColor = UIColor.white.cgColor
      
        self.imageView19.layer.cornerRadius = self.imageView19.frame.size.width / 2;
        self.imageView19.clipsToBounds = true;
        self.imageView19.layer.borderWidth = 2.0
        self.imageView19.layer.borderColor = UIColor.white.cgColor
      
        self.imageView20.layer.cornerRadius = self.imageView20.frame.size.width / 2;
        self.imageView20.clipsToBounds = true;
        self.imageView20.layer.borderWidth = 2.0
        self.imageView20.layer.borderColor = UIColor.white.cgColor
      
        self.imageView21.layer.cornerRadius = self.imageView21.frame.size.width / 2;
        self.imageView21.clipsToBounds = true;
        self.imageView21.layer.borderWidth = 2.0
        self.imageView21.layer.borderColor = UIColor.white.cgColor
      
        self.imageView22.layer.cornerRadius = self.imageView22.frame.size.width / 2;
        self.imageView22.clipsToBounds = true;
        self.imageView22.layer.borderWidth = 2.0
        self.imageView22.layer.borderColor = UIColor.white.cgColor
      
        self.imageView23.layer.cornerRadius = self.imageView23.frame.size.width / 2;
        self.imageView23.clipsToBounds = true;
        self.imageView23.layer.borderWidth = 2.0
        self.imageView23.layer.borderColor = UIColor.white.cgColor
      
        self.imageView24.layer.cornerRadius = self.imageView24.frame.size.width / 2;
        self.imageView24.clipsToBounds = true;
        self.imageView24.layer.borderWidth = 2.0
        self.imageView24.layer.borderColor = UIColor.white.cgColor
      
        self.imageView25.layer.cornerRadius = self.imageView25.frame.size.width / 2;
        self.imageView25.clipsToBounds = true;
        self.imageView25.layer.borderWidth = 2.0
        self.imageView25.layer.borderColor = UIColor.white.cgColor
      
        self.imageView26.layer.cornerRadius = self.imageView26.frame.size.width / 2;
        self.imageView26.clipsToBounds = true;
        self.imageView26.layer.borderWidth = 2.0
        self.imageView26.layer.borderColor = UIColor.white.cgColor
      
        self.imageView27.layer.cornerRadius = self.imageView27.frame.size.width / 2;
        self.imageView27.clipsToBounds = true;
        self.imageView27.layer.borderWidth = 2.0
        self.imageView27.layer.borderColor = UIColor.white.cgColor
      
        self.imageView28.layer.cornerRadius = self.imageView28.frame.size.width / 2;
        self.imageView28.clipsToBounds = true;
        self.imageView28.layer.borderWidth = 2.0
        self.imageView28.layer.borderColor = UIColor.white.cgColor
      
        self.imageView29.layer.cornerRadius = self.imageView29.frame.size.width / 2;
        self.imageView29.clipsToBounds = true;
        self.imageView29.layer.borderWidth = 2.0
        self.imageView29.layer.borderColor = UIColor.white.cgColor
      
        self.imageView30.layer.cornerRadius = self.imageView30.frame.size.width / 2;
        self.imageView30.clipsToBounds = true;
        self.imageView30.layer.borderWidth = 2.0
        self.imageView30.layer.borderColor = UIColor.white.cgColor
      
        self.imageView31.layer.cornerRadius = self.imageView31.frame.size.width / 2;
        self.imageView31.clipsToBounds = true;
        self.imageView31.layer.borderWidth = 2.0
        self.imageView31.layer.borderColor = UIColor.white.cgColor
      
        self.imageView32.layer.cornerRadius = self.imageView32.frame.size.width / 2;
        self.imageView32.clipsToBounds = true;
        self.imageView32.layer.borderWidth = 2.0
        self.imageView32.layer.borderColor = UIColor.white.cgColor
      
        self.imageView33.layer.cornerRadius = self.imageView33.frame.size.width / 2;
        self.imageView33.clipsToBounds = true;
        self.imageView33.layer.borderWidth = 2.0
        self.imageView33.layer.borderColor = UIColor.white.cgColor
      
        self.imageView34.layer.cornerRadius = self.imageView34.frame.size.width / 2;
        self.imageView34.clipsToBounds = true;
        self.imageView34.layer.borderWidth = 2.0
        self.imageView34.layer.borderColor = UIColor.white.cgColor
      
        self.imageView35.layer.cornerRadius = self.imageView35.frame.size.width / 2;
        self.imageView35.clipsToBounds = true;
        self.imageView35.layer.borderWidth = 2.0
        self.imageView35.layer.borderColor = UIColor.white.cgColor
      
        self.imageView36.layer.cornerRadius = self.imageView36.frame.size.width / 2;
        self.imageView36.clipsToBounds = true;
        self.imageView36.layer.borderWidth = 2.0
        self.imageView36.layer.borderColor = UIColor.white.cgColor
      
        self.imageView37.layer.cornerRadius = self.imageView37.frame.size.width / 2;
        self.imageView37.clipsToBounds = true;
        self.imageView37.layer.borderWidth = 2.0
        self.imageView37.layer.borderColor = UIColor.white.cgColor
      
        self.imageView38.layer.cornerRadius = self.imageView38.frame.size.width / 2;
        self.imageView38.clipsToBounds = true;
        self.imageView38.layer.borderWidth = 2.0
        self.imageView38.layer.borderColor = UIColor.white.cgColor
      
        self.imageView39.layer.cornerRadius = self.imageView39.frame.size.width / 2;
        self.imageView39.clipsToBounds = true;
        self.imageView39.layer.borderWidth = 2.0
        self.imageView39.layer.borderColor = UIColor.white.cgColor
      
        self.imageView40.layer.cornerRadius = self.imageView40.frame.size.width / 2;
        self.imageView40.clipsToBounds = true;
        self.imageView40.layer.borderWidth = 2.0
        self.imageView40.layer.borderColor = UIColor.white.cgColor
      
        self.imageView41.layer.cornerRadius = self.imageView41.frame.size.width / 2;
        self.imageView41.clipsToBounds = true;
        self.imageView41.layer.borderWidth = 2.0
        self.imageView41.layer.borderColor = UIColor.white.cgColor
      
        self.imageView42.layer.cornerRadius = self.imageView42.frame.size.width / 2;
        self.imageView42.clipsToBounds = true;
        self.imageView42.layer.borderWidth = 2.0
        self.imageView42.layer.borderColor = UIColor.white.cgColor
      
        self.imageView43.layer.cornerRadius = self.imageView43.frame.size.width / 2;
        self.imageView43.clipsToBounds = true;
        self.imageView43.layer.borderWidth = 2.0
        self.imageView43.layer.borderColor = UIColor.white.cgColor
      
        self.imageView44.layer.cornerRadius = self.imageView44.frame.size.width / 2;
        self.imageView44.clipsToBounds = true;
        self.imageView44.layer.borderWidth = 2.0
        self.imageView44.layer.borderColor = UIColor.white.cgColor
      
        self.imageView45.layer.cornerRadius = self.imageView45.frame.size.width / 2;
        self.imageView45.clipsToBounds = true;
        self.imageView45.layer.borderWidth = 2.0
        self.imageView45.layer.borderColor = UIColor.white.cgColor
      
      
        //ANC gradient main app background image
        backgroundImage.contentMode = .scaleAspectFill
        tableView.backgroundView = backgroundImage
      
        }

    }


Ok, thanks for showing your code.

So, you designed your UITableViewController as Static Cells, and you add 45 rows, and put UIImageViews onto each row.

And then you connected the image views to the outlets, one by one. Right?

Yes. Then, under viewDidLoad, I have the previous code I showed you for each image. It has worked flawlessly until the migration to Swift 5. Now the images are no longer circular, they are square. Nothing was changed, just the migration.

Thanks for confirmation, I have reduced your code to 5 rows, and all the image views are shown as circular.


One possibility, some of the outlet connections are broken while migrating. (Xcode may show fake connections even when some of them are broken.)


You may try disconnect and re-connect the non-circular image views.


Or re-designing your table view to use Dynamic Prototypes can be a good alternative.

Table view with 45 (or more?) Static Cells is not an ordinary design.

Thank you.