图像视图双边框 [英] Image View Double Border
问题描述
我正在调用一个设置UIImageView的函数:
I'm calling a function that sets up a UIImageView:
func setupImageView(_ imageView: UIImageView) {}
我想给UIImageView一个图像,将其圆角化,并给它两个不同的边框.
I want to give that UIImageView an image, round its corners, and give it two different borders.
这是我目前正在做的事情:
Here is what I am currently doing:
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颜色蓝色的最佳方法是什么?
What is the best way to apply a second borderColor of color blue around the white border?
我尝试将子层创建为CALayer,并为其赋予蓝色边框,但这位于图像后面以及白色边框内部.我还尝试绘制UIBezierPath,但是它也留在白色边框内.
I tried creating a sublayer as a CALayer and giving it a blue border, but this goes behind the image, and also inside of the white border. I also tried drawing a UIBezierPath, but that stays inside of the white border as well.
推荐答案
如果您想减小线条的大小,可以在图像视图的后面添加UIlabel或UIImageview,其尺寸比图像视图大一点,并应用拐角半径.您的代码(请检查下面的代码)
You can add UIlabel or UIImageview at back of your image view having size little bit larger than your image view and applying corner radius, if you want to reduce line of your code (Please check below code)
imgview.layer.masksToBounds = true
imgview.layer.cornerRadius = imgview.frame.size.width/2
imgview.layer.borderWidth = 5
imgview.layer.borderColor = UIColor.white.cgColor
以编程方式在已拍摄的图像视图的背面添加新的图像视图
Add new image view programatically at back side of image view already taken
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)
我知道它不是适当的解决方案,但是我们可以使用它来减少代码行希望对您有帮助
I know its not proper solution but we can use this to reduce lines of code Hope it will helps you
这篇关于图像视图双边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!