Circular Image Becoming a Diamond Not Circle - Swift 3

后端 未结 3 1374
天涯浪人
天涯浪人 2021-01-21 06:10

I have been trying to create a circular image but for some reason it has been outputting as a diamond. This is my code:

override func viewDidLoad() {
    display         


        
相关标签:
3条回答
  • 2021-01-21 06:18

    I had the same problem but because I had copied and pasted the cell from another view, the cell class was giving it instructions meant for another cell.

    0 讨论(0)
  • 2021-01-21 06:21

    The issue is that viewWillLayoutSubviews is too early in the layout process. You can do it in viewDidLayoutSubviews.

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        profilePic.layer.cornerRadius = min(profilePic.frame.width, profilePic.frame.height) / 2
        profilePic.clipsToBounds = true
    }
    

    Or, alternatively, you can use a designable view, and let the view take care of it:

    @IBDesignable class RoundedImageView: UIImageView {
        override func layoutSubviews() {
            super.layoutSubviews()
            layer.cornerRadius = min(bounds.width, bounds.height) / 2
        }
    }
    

    And by making it designable, you can see it rendered with rounded corners right in Interface Builder.

    0 讨论(0)
  • 2021-01-21 06:24

    Your problem it is probably caused by some constraints you have added to your imageView. Try adding only 4 constraints, 1 for fixed width, 1 for fixed height, 1 for leading space and 1 for top space as you can see at the screenshot below:


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