How to keep a round imageView round using auto layout?

后端 未结 15 1383
悲&欢浪女
悲&欢浪女 2020-12-01 06:22

How do I turn a rectangular image view into a circular image view that can hold shape in auto layout without setting width and height restraints? Thereby allowing the imageV

15条回答
  •  再見小時候
    2020-12-01 06:39

    Can be easily done by creating an IBOutlet for the constraint which needs to be changed at runtime. Below is the code:

    1. Create a IBOutlet for the constraint that needs to be changed at run time.

      @IBOutlet var widthConstraint: NSLayoutConstraint!
      
    2. Add below code in viewDidLoad():

      self.widthConstraint.constant = imageWidthConstraintConstant()
      

    Below function determines for device types change the width constraint accordingly.

    func imageWidthConstraintConstant() -> CGFloat {
    
        switch(self.screenWidth()) {
    
        case 375:
            return 100
    
        case 414:
            return 120
    
        case 320:
            return 77
    
        default:
            return 77
        }
    }
    

提交回复
热议问题