gpt4 book ai didi

iOS swift 如何将 imageview 准确地放置在 uiview 中

转载 作者:行者123 更新时间:2023-11-29 00:54:57 27 4
gpt4 key购买 nike

我有一个问题,我正在以编程方式创建一个 imageView,然后将其添加到中心 View ,这是可行的。但问题是,并没有占据中心 View 中的整个空间,它在 uiview 中看起来是的,但没有覆盖所有空间,总是有点向下。有帮助吗?

代码:

//let backgroundImage = UIImageView(frame: centerView.frame)
let backgroundImage: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.centerView.bounds.size.width, height: self.centerView.bounds.size.height))
print("backgorundImage coordinates: \(backgroundImage.frame)")
backgroundImage.image = drawOverImage
backgroundImage.sizeThatFits(CGSizeMake(centerView.bounds.width, self.centerView.bounds.height))
//check this the image is being drawn bottom because is the fame for the previous 0.0
//backgroundImage.autoPinEdgeToSuperviewMargin(ALEdge.Top, relation: NSLayoutRelation.Equal)
//backgroundImage.contentMode = UIViewContentMode.ScaleAspectFill //too big
//backgroundImage.contentMode = UIViewContentMode.ScaleAspectFit //sama
backgroundImage.contentMode = UIViewContentMode.ScaleAspectFill
backgroundImage.clipsToBounds = true
//imageView.image = background
backgroundImage.center = view.center

let coordinatesForImage: CGRect = self.view.convertRect(backgroundImage.frame, toView: centerView)
let pointOfImage: CGPoint = backgroundImage.convertPoint(self.centerView.frame.origin, toView: backgroundImage)

print("coordinates test: \(coordinatesForImage)")
print("point x: \(pointOfImage.x)")
print("point y: \(pointOfImage.y)")

//backgroundImage.contentMode = UIViewContentMode.ScaleToFill
self.centerView.insertSubview(backgroundImage, atIndex: 0)


let pointOfImageToSuperView: CGPoint = (backgroundImage.superview?.convertPoint(backgroundImage.center, toView: self.centerView))!

print("superview imagepoint: \(pointOfImageToSuperView)")

评论就是我想做的事情。

编辑:这就是正在发生的事情。

Missing bottom

我从底部漏掉了一点,现在我不知道是图像的大小还是什么,我可以更改 uiview 的大小以匹配图像吗?

最佳答案

简单地尝试一下:

let backgroundImage: UIImageView = UIImageView(frame: self.centerView.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.contentMode = .ScaleAspectFill
self.centerView.addSubview(backgroundImage)

关于iOS swift 如何将 imageview 准确地放置在 uiview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37683820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com