图像视图双边框

时间:2017-12-07 21:03:40

标签: ios swift border calayer

我正在调用一个设置UIImageView的函数:

func setupImageView(_ imageView: UIImageView) {}

我想给UIImageView一个图像,围绕它的角落,给它两个不同的边框。

以下是我目前正在做的事情:

imageView.image = imageConstants.imageThatIsWanted
imageView.clipsToBounds = true
imageView.layer.cornerRadius = imageView.frame.height / 2
imageView.layer.borderWidth = 3.0
imageView.layer.borderColor = UIColor.white.cgColor

在白色边框周围应用第二个borderColor蓝色的最佳方法是什么?

我尝试创建一个子层作为CALayer并给它一个蓝色边框,但这是在图像后面,也在白色边框内部。我也尝试绘制一个UIBezierPath,但它也保留在白色边框内。

2 个答案:

答案 0 :(得分:0)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var secondView: UIView!
    @IBOutlet weak var imgView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        imgView.layer.cornerRadius = imgView.frame.size.height/2
         secondView.layer.cornerRadius = secondView.frame.size.height/2
        imgView.layer.borderWidth = 5.0
        imgView.layer.borderColor = UIColor.red.cgColor
        imgView.clipsToBounds = true
         secondView.clipsToBounds = true
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

enter image description here

答案 1 :(得分:0)

如果要减少代码行(请检查下面的代码),可以在图像视图的后面添加UIlabel或UIImageview,其尺寸比图像视图大一点,并应用转角半径。

imgview.layer.masksToBounds = true
    imgview.layer.cornerRadius = imgview.frame.size.width/2
    imgview.layer.borderWidth = 5
    imgview.layer.borderColor = UIColor.white.cgColor

以编程方式在已拍摄的图像视图的背面添加新的图像视图

let img = UIImageView(frame: CGRect(x: imgview.frame.origin.x - 2, y: imgview.frame.origin.y - 2, width: imgview.frame.size.width + 4, height: imgview.frame.size.height + 4))
    img.layer.masksToBounds = true
    img.layer.cornerRadius = img.frame.size.width/2
//img.backgroundColor = UIColor.blue // You can also use background color instead of border
img.layer.borderWidth = 5
    img.layer.borderColor = UIColor.blue.cgColor
    self.view.addSubview(img)
    self.view.sendSubview(toBack: img)

我知道它不是适当的解决方案,但是我们可以使用它来减少代码行 希望对您有帮助