Ios 如何将UIview添加到imageView,同时确保UIview的内容位于it';什么样的框架?
我有一个superview,其中包含一个Ios 如何将UIview添加到imageView,同时确保UIview的内容位于it';什么样的框架?,ios,swift,uiview,ios8,addsubview,Ios,Swift,Uiview,Ios8,Addsubview,我有一个superview,其中包含一个图像视图、一个测试视图,以及一些按钮。我创建了一个UIView,其中包含ImageView2和一个removebutton。 当我将UIView添加到UIImageView时,UIView的内容不在UIView的框架中。因此,我无法应用平移手势识别器。 我已尝试应用约束并更改ImageView2的帧和中心 下面是我用来创建上述UIView的代码。如何确保ui视图的内容在ui视图的框架中可见 let rect = CGRect(x: self.imageVi
图像视图
、一个测试视图
,以及一些按钮。我创建了一个UIView
,其中包含ImageView2
和一个removebutton
。
当我将UIView
添加到UIImageView
时,UIView
的内容不在UIView
的框架中。因此,我无法应用平移手势识别器。
我已尝试应用约束并更改ImageView2
的帧和中心
下面是我用来创建上述UIView
的代码。如何确保ui视图的内容在ui视图的框架中可见
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)
问题是,这样的行与您的想法不符:
let rect = CGRect(x: self.imageView.center.x-30, y: self.imageView.center.y-30, width: 80, height: 80)
// ... and ...
imageView2.center = testview.center
视图的中心
位于其框架
坐标中-它与视图在其superview中的放置方式有关。但是子视图的位置必须根据其超级视图边界
来描述,而不是其框架
。它的边界
是它的内部坐标,这是您想要的
让我们看一个最简单的例子:如何在超级视图中居中子视图。不是这样的:
imageView2.center = testview.center
你看到问题了吗testview.center
是testview
在其superview中的位置;它不是您想要的testview
的内部中心。testview
的内部中心来自其bounds
。这是一种方式:
let c = CGPointMake(testview.bounds.midX, testview.bounds.midY)
imageView2.center = c
“UIView的内容不在UIView的框架内”不清楚这意味着什么。您能更清楚地解释一下问题所在吗?uiview的子视图(imageView)超出了uiview的框架/边界。例如,如果ui视图有一个x:0,y:0,宽度:80,高度:80的框架,那么imageview是在x:200,y:200:width:60高度:60,你理解框架和边界之间的区别吗?Thanx matt为我指出了正确的方向:)。你能帮我做一件事吗?例如,我有一个选择HidesBottomBar当按下时(在情节提要中)这会从视图中删除选项卡栏,但当视图出现时,它会用动画来实现这一点。我不想要那个动画,所以有没有一种方法可以在viewDidLoad方法中做到这一点。