ios - 如何使边框距图像 x 距离?

标签 ios swift

我想要图像周围有边框,但我希望边框与实际图像有一定的距离。现在它在图像周围创建了一个边框。我该怎么做?

 let image = UIImage(named: "WhitePeeyr")!
    let imageView = UIImageView(image: image)
    let padding = CGFloat(20)
    let frame = CGRectMake(0, 0, 100 + 2*padding, 40 + 2*padding)


    imageView.frame = CGRectInset(frame, padding, padding)
    imageView.frame.origin.y = view.frame.size.height - 542
    imageView.frame.origin.x = (self.view.bounds.size.width - imageView.frame.size.width) / 2.0
    view.addSubview(imageView)

    let borderView = UIView(frame: frame)
    borderView.layer.borderWidth = 2.0
    borderView.layer.borderColor = UIColor.whiteColor().CGColor
    borderView.clipsToBounds = true
    view.addSubview(borderView)

最佳答案

您将需要两个单独的 View (或层):

let image = UIImage(named: "image")!
let padding = CGFloat(20)
let frame = CGRectMake(0, 0, image.size.width + 2*padding, image.size.height + 2*padding)

let borderView = UIView(frame: frame)
borderView.layer.borderWidth = 2.0
borderView.layer.borderColor = UIColor.whiteColor().CGColor
borderView.clipsToBounds = true
view.addSubview(borderView)

let imageView = UIImageView(image: image)
imageView.frame = CGRectInset(frame, padding, padding)
view.addSubview(imageView)

关于ios - 如何使边框距图像 x 距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640582/

相关文章:

ios - 在 Swift 中转换/转换字典

ios - 在 Swift 中获取 UIcollectionView 中的下一个单元格

ios - 更新另一个 View 上的标签

ios - 为 iOS 配置 Firebase/Messaging 的问题

ios - 关闭键盘迫使我长按表格 View 中的单元格

ios - 找不到命名空间中的Xamarin.Forms类(仅在iOS上)

ios - 调整 UITableView Cell 上的 SubView 约束

ios - 具有多个 DrawRect 方法的 UIView

iOS 13 导航栏大标题问题

ios - `_ = something`是什么意思?