Ios 按程序制作一个圆形按钮

Ios 按程序制作一个圆形按钮,ios,swift,uibutton,autolayout,uikit,Ios,Swift,Uibutton,Autolayout,Uikit,我想在iOS应用程序中创建一个圆形UIButton 该按钮用于为用户创建配置文件图片 这是圆形剖面图: 这就是选择图片后的样子: 你可以看到这个按钮太大了,不是圆形的。 这是我的代码: func setUpProfilePicture() { profileIcon = UIImage(named: "characteer")! profilePicture.setImage(profileIcon, for: .normal)

我想在iOS应用程序中创建一个圆形UIButton

该按钮用于为用户创建配置文件图片

这是圆形剖面图:

这就是选择图片后的样子:

你可以看到这个按钮太大了,不是圆形的。 这是我的代码:

func setUpProfilePicture() {
        profileIcon = UIImage(named: "characteer")!
        profilePicture.setImage(profileIcon, for: .normal)
        profilePicture.contentMode = .scaleAspectFill
        profilePicture.addTarget(self, action: #selector(handleSelectedPhoto), for: .touchUpInside)
        
        self.view.addSubview(profilePicture)
        
        profilePicture.translatesAutoresizingMaskIntoConstraints = false
        profilePicture.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        profilePicture.topAnchor.constraint(equalTo: view.topAnchor, constant: -180).isActive = true
        profilePicture.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 50).isActive = true
        profilePicture.heightAnchor.constraint(equalTo: view.heightAnchor, constant: 50).isActive = true
    }
  
所选图片应刚好填充在配置文件图片字符图像的圆圈中。我使用自动布局,所以我找到的大多数教程对我没有帮助


谢谢

调整UIButton的图层:

profilePicture.imageView.contentMode = .scaleAspectFit
let cornerRadius = 25 // 50 * 0.5
profilePicture.layer.cornerRadius = cornerRadius
profilePicture.layer.masksToBounds = true

调整UIButton的图层:

profilePicture.imageView.contentMode = .scaleAspectFit
let cornerRadius = 25 // 50 * 0.5
profilePicture.layer.cornerRadius = cornerRadius
profilePicture.layer.masksToBounds = true

您需要将UIButton的角半径更改为其大小的一半

profileIcon.imageView?.contentMode = .scaleAspectFit
profileIcon.layer.cornerRadius = min(profileIcon.frame.height, profileIcon.frame.width) / 2
profileIcon.layer.masksToBounds = true

您需要将UIButton的角半径更改为其大小的一半

profileIcon.imageView?.contentMode = .scaleAspectFit
profileIcon.layer.cornerRadius = min(profileIcon.frame.height, profileIcon.frame.width) / 2
profileIcon.layer.masksToBounds = true
以编程方式:

private extension UIView { 
    func willCircle() {
    self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) * 0.5
    self.layer.masksToBounds = true
    self.clipsToBounds = true
    }
}
用法:profilePicture.willCircle 故事板:

@IBInspectable
var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
}
以编程方式:

private extension UIView { 
    func willCircle() {
    self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) * 0.5
    self.layer.masksToBounds = true
    self.clipsToBounds = true
    }
}
用法:profilePicture.willCircle 故事板:

@IBInspectable
var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
}

它仍然不是一个圆,但它很小。我认为你应该把我的答案标记为正确,因为在我的回答中,圆角半径会根据按钮的不同而变化,这样其他人就可以很容易地复制我的答案answer@TLGCodin“我可以,没什么大不了的,但在你的情况下,框架必须经过计算。因此,你必须使用约束或检查框架是否为零,否则将强制布局。它仍然不是圆:但它很小。我认为你应该将我的答案标记为正确,因为在我的答案中,角半径将根据按钮而改变,以便其他人可以轻松复制我的答案answer@TLGCodin“我能,没什么大不了的,但在你的情况下,必须计算框架。所以你必须使用约束或检查框架是否为零,否则将强制布局。对不起,兄弟,这不起作用!没有发生什么事对不起,兄弟,但它不起作用!什么也没有发生