How to set image in circle in swift

前端 未结 14 1756
星月不相逢
星月不相逢 2020-12-04 09:55

How can i make i circle picture with swift ?

My ViewController :

import UIKit
import Foundation

class FriendsViewController : UIViewController{

            


        
14条回答
  •  情话喂你
    2020-12-04 10:05

    This way is the least expensive way and always keeps your image view rounded:

    class RoundedImageView: UIImageView {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            clipsToBounds = true
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            clipsToBounds = true
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            assert(bounds.height == bounds.width, "The aspect ratio isn't 1/1. You can never round this image view!")
    
            layer.cornerRadius = bounds.height / 2
        }
    }
    

    The other answers are telling you to make views rounded based on frame calculations set in a UIViewControllers viewDidLoad() method. This isn't correct, since it isn't sure what the final frame will be.

提交回复
热议问题