如何在 swift 4 中使图像视图变圆
How to make image view round in swift 4
虽然网上有这个问题的解决方法,但是我做不到,想做个圆图
我正在使用的代码:
extension UIImageView {
func makeRounded() {
let radius = self.frame.width/2.0
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
然后我在 viewdidload() 中调用此函数,如 imgvw.makeRounded()。但它不会来。请帮忙
之前的link对我没有帮助
重写 viewDidLayoutSubviews
将不必要地调用函数 makeRounded()
因为每次在超级视图中发生某些布局时都会调用它。你应该使用这个:
class RoundedImageView: UIImageView {
@override func layoutSubviews() {
super.layoutSubviews()
let radius = self.frame.width/2.0
layer.cornerRadius = radius
clipsToBounds = true // This could get called in the (requiered) initializer
// or, ofcourse, in the interface builder if you are working with storyboards
}
}
将 imageView 的 class 设置为 RoundedImageView
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var image: UIImageView!
func makeRounded() {
image.layer.borderWidth = 1
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.blackColor().CGColor
image.layer.cornerRadius = image.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
image.clipsToBounds = true
}
快乐编码
为您的 class
创建一个扩展
extension ViewController: UIViewController{
func makeRounded() {
layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.blackColor().CGColor
layer.cornerRadius = frame.height/2
clipsToBounds = true
}
}
然后调用使用它
imageView.makeRounded()
我的代码运行良好。
avatar.layer.borderWidth = 1
avatar.layer.masksToBounds = false
avatar.layer.borderColor = UIColor(hexString: "#39B44E").cgColor
avatar.layer.cornerRadius = avatar.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
avatar.clipsToBounds = true
我正在使用的代码:
extension UIImageView {
func makeRounded() {
let radius = self.frame.width/2.0
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
然后我在 viewdidload() 中调用此函数,如 imgvw.makeRounded()。但它不会来。请帮忙
之前的link对我没有帮助
重写 viewDidLayoutSubviews
将不必要地调用函数 makeRounded()
因为每次在超级视图中发生某些布局时都会调用它。你应该使用这个:
class RoundedImageView: UIImageView {
@override func layoutSubviews() {
super.layoutSubviews()
let radius = self.frame.width/2.0
layer.cornerRadius = radius
clipsToBounds = true // This could get called in the (requiered) initializer
// or, ofcourse, in the interface builder if you are working with storyboards
}
}
将 imageView 的 class 设置为 RoundedImageView
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var image: UIImageView!
func makeRounded() {
image.layer.borderWidth = 1
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.blackColor().CGColor
image.layer.cornerRadius = image.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
image.clipsToBounds = true
}
快乐编码
为您的 class
创建一个扩展extension ViewController: UIViewController{
func makeRounded() {
layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.blackColor().CGColor
layer.cornerRadius = frame.height/2
clipsToBounds = true
}
}
然后调用使用它
imageView.makeRounded()
我的代码运行良好。
avatar.layer.borderWidth = 1
avatar.layer.masksToBounds = false
avatar.layer.borderColor = UIColor(hexString: "#39B44E").cgColor
avatar.layer.cornerRadius = avatar.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
avatar.clipsToBounds = true