Ios 图像视图双边框
我正在调用设置UIImageView的函数:Ios 图像视图双边框,ios,swift,border,calayer,Ios,Swift,Border,Calayer,我正在调用设置UIImageView的函数: func setupImageView(_ imageView: UIImageView) {} 我想给UIImageView一个图像,绕过它的角,给它两个不同的边界 以下是我目前正在做的事情: imageView.image = imageConstants.imageThatIsWanted imageView.clipsToBounds = true imageView.layer.cornerRadius = imageView.frame
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
在白色边框周围应用第二种边框颜色蓝色的最佳方法是什么
我尝试创建一个子层作为CALayer,并给它一个蓝色边框,但这在图像后面,也在白色边框的内部。我还试着画了一个UIBezierPath,但它也停留在白色边框内
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.
}
}
如果您想减少代码行,可以在图像视图后面添加UIlabel或UIImageview,其大小略大于图像视图,并应用圆角半径(请检查下面的代码) 以编程方式在已拍摄图像视图的背面添加新图像视图
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)
我知道这不是合适的解决方案,但我们可以使用它来减少代码行数
希望它能帮助您您能将图像视图设置为更大的
ui视图
的子视图吗?一个高度/宽度多6点且有蓝色边框的?
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)