Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift 4中创建图像视图圆形_Ios_Swift_Uiimageview_Uiimage_Swift4 - Fatal编程技术网

Ios 如何在swift 4中创建图像视图圆形

Ios 如何在swift 4中创建图像视图圆形,ios,swift,uiimageview,uiimage,swift4,Ios,Swift,Uiimageview,Uiimage,Swift4,虽然这个问题在互联网上有解决的办法,但我不能这样做,我想圆一个图像 我使用的代码如下: extension UIImageView { func makeRounded() { let radius = self.frame.width/2.0 self.layer.cornerRadius = radius self.layer.masksToBounds = true } } 然后我在viewdidload(

虽然这个问题在互联网上有解决的办法,但我不能这样做,我想圆一个图像

我使用的代码如下:

extension UIImageView {        
    func makeRounded() {
        let radius = self.frame.width/2.0
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }
 }
然后我在viewdidload()中调用这个函数,就像imgvw.makeRounded()一样。但它不会到来。请帮忙


上一个链接对我没有帮助

重写
viewDidLayoutSubviews
将无需调用函数
makeRounded()
,因为每次在superview中出现布局时都会调用该函数。您应该使用以下选项:

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的类设置为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
}

快乐编码

为您的类创建扩展

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()

调用ViewDidDisplay并检查set
self.clipToBounds=true的可能重复项
在viewDidLayoutSubView方法中调用此函数可能是frame.width=0->半径=0,让我们在ViewDidDisplay或viewDidLayoutSubView中调用它。它不会出现。您是否更改了cornerRadius的值,比如set.cornerRadius=4.0。试着根据你的需要改变这个值,它是方的,先生,我要圆的是。。问题是关于图像本身,而不是它何时添加到controlleritsir@GoribDeveloper如果你复制粘贴此代码!和将UIImageView的类设置为RoundedImageView,它应该是四舍五入的