How to keep a round imageView round using auto layout?

后端 未结 15 1376
悲&欢浪女
悲&欢浪女 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:47

    Add a 1:1 aspect ratio constraint to the imageView for it to remain circular, despite any height or width changes.

    0 讨论(0)
  • 2020-12-01 06:50

    If you have subclassed the UIImageView. Then just add this piece of magical code in it.

    Written in : Swift 3

    override func layoutSubviews() {
        super.layoutSubviews()
        if self.isCircular! {
            self.layer.cornerRadius = self.bounds.size.width * 0.50
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:54

    create new interface in your .h file like

    @interface CornerClip : UIImageView
    
    @end
    

    and implementation in .m file like

    @implementation cornerClip
    
    
    -(void)layoutSubviews
    {
        [super layoutSubviews];
    
        CGFloat radius = self.bounds.size.width / 2.0;
    
        self.layer.cornerRadius = radius;
    
    }
    
    @end
    

    now just give class as "CornerClip" to your imageview. 100% working... Enjoy

    0 讨论(0)
提交回复
热议问题