【发布时间】:2016-06-07 15:44:03
【问题描述】:
我有一个问题,我正在以编程方式创建一个 imageView,然后将它添加到一个中心视图,这有点工作。但问题是它没有占据中心视图的整个空间,它在 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)")
cmets 是我想做的所有事情。
编辑: 这就是正在发生的事情。
我从底部遗漏了一点,现在我不知道是图像的大小还是什么,我可以更改uiview的大小以匹配图像吗?
【问题讨论】:
-
您正在那里进行大量的框架操作...如果您希望它准确地覆盖超级视图(
centerView),您需要做的就是像您一样设置框架第一行。不要这样做sizeThatFits。不要这样做*.center = *.center。不要从一种视图到另一种视图进行点或矩形转换。等等......您的第一行告诉视图“与其他视图一样大并从(0,0)开始”。然后将其添加为子视图,它将完美地定位在它的父视图中 (centerView)。 -
嗯,我想要它,但有没有办法告诉 centerView 我想要你和你之后的图像一样大?
标签: ios swift uiview uiimageview