如何在确保UIView的内容位于其框架内的同时将UIview添加到imageView? [英] How do I add a UIview to an imageView while ensuring that the contents of the UIView are inside the it's frame?

查看:148
本文介绍了如何在确保UIView的内容位于其框架内的同时将UIview添加到imageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个superview,其中包含 ImageView ,一个 testView ,以及一些按钮。我创建了一个 UIView ,其中包含 ImageView2 和一个 removebutton
当我将 UIView 添加到 UIImageView 时, UIView的内容不在 UIView 的框架内。因此,我无法应用平移手势识别器。
我试图应用约束并更改 ImageView2 的框架和中心。

I have a superview which contains a ImageView, a testView, and some buttons. I created a UIView which contains ImageView2 and a removebutton. When I add UIView to UIImageView, the contents of UIView are not in the frame of UIView. Because of that, I cannot apply pan gesture recognizer. I have tried to apply constraints and to change the frame and centre of ImageView2.

下面是我用来创建上面提到的 UIView 的代码。如何确保 UIView 的内容在 UIView 的框架中可见?

Below is the code I am using to create the above mentioned UIView. How do I ensure that the contents of the UIView are visible in the frame of the UIView?

let rect = CGRect(x: self.imageView.center.x-30, y: self.imageView.center.y-30, width: 80, height: 80)

testview = UIView(frame: rect)
testview.layer.cornerRadius = testview.frame.width/2
testview.backgroundColor = UIColor.blackColor()
testview.clipsToBounds = true

imageView2 = UIImageView(frame: CGRect(x: testview.frame.minX+10, y: testview.frame.minY+10, width: 60, height: 60))

let rect2 = CGRect(x: self.testview.frame.maxX-17, y: self.testview.frame.minY, width: 20, height: 20)

removeButton = UIButton(frame: rect2)

removeButton.layer.cornerRadius = removeButton.frame.width/2

removeButton.backgroundColor = UIColor.blackColor()

removeButton.clipsToBounds = true

imageView2.layer.cornerRadius = imageView2.frame.width/2

imageView2.userInteractionEnabled = true

imageView2.clipsToBounds = true
testview.alpha = 0.3

imageView2.center = testview.center

imageView2.center = testview.center

testview.addSubview(imageView2)

testview.addSubview(removeButton)

testview.bringSubviewToFront(imageView2)

imageView.addSubview(testview)


推荐答案

问题是这样的行不符合你的想法:

The problem is that lines like this do not do what you think:

let rect = CGRect(x: self.imageView.center.x-30, y: self.imageView.center.y-30, width: 80, height: 80)
// ... and ...
imageView2.center = testview.center

视图的中心在其 frame 坐标 - 它与视图在其superview中放置的方式有关。但是子视图的位置必须用它的超级视图 bounds 来描述,而不是它的框架。它的界限是它的内部坐标,这就是你想要的。

The center of a view is in its frame coordinates - it has to do with how the view is placed in its superview. But the position of a subview must be described in terms of its superviews bounds, not its frame. Its bounds are its internal coordinates, which is what you want.

我们来看看最简单的情况:如何在超视图中居中子视图。这不是这样的:

Let's take the simplest case: how to center a subview in a superview. This is not the way:

imageView2.center = testview.center

你看到了问题吗? testview.center testview 位于其superview中的 ;它不是 testview的内部中心,这就是你想要的。 testview 的内部中心来自其边界。这是这样的:

You see the problem? testview.center is where testview is located in its superview; it is not the internal center of testview, which is what you want. The internal center of testview comes from its bounds. This is the way:

let c = CGPointMake(testview.bounds.midX, testview.bounds.midY)
imageView2.center = c

这篇关于如何在确保UIView的内容位于其框架内的同时将UIview添加到imageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆