简体   繁体   English

iOS快速如何将图像视图准确地放置在uiview中

[英]iOS swift how to place an imageview in a uiview exactly

I have an issue, I'm creating an imageView programmatically and then add it to a center view, which is kind of working. 我有一个问题,我正在以编程方式创建imageView,然后将其添加到中心视图,这是可行的。 But the problem is that is not taking the whole space in the center view, it appears yes in the uiview but not covering all always a bit down. 但是问题是没有在中心视图中占据整个空间,在uiview中看起来是肯定的,但是并没有覆盖所有的空间。 Any help? 有什么帮助吗?

The code: 编码:

//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)")

The comments are all the thing I'm trying to do. 评论就是我要做的所有事情。

EDIT: This is what is happening. 编辑:这是正在发生的事情。

缺少底部

I missing a little bit from the bottom, now I don't know if is the size of the image or what, could i change the size of the uiview to match the image? 我从底部开始遗漏了一点,现在我不知道图像的大小还是什么,我可以更改uiview的大小以匹配图像吗?

Simply try: 只需尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM