ios 如何在另一个视图中获取视图的框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8082493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the frame of a view inside another view?
提问by cyclingIsBetter
I have an UIImageView
in the self.view
(the main View) and inside it there is a UIButton
. I want to know what's the frame of UIButton
in self.view
not in UIImageView
.
我UIImageView
在self.view
(主视图)中有一个,里面有一个UIButton
. 我想知道UIButton
in self.view
not in的框架是什么UIImageView
。
回答by beryllium
I guess you are looking for this method
我猜你正在寻找这种方法
// Swift
let frame = imageView.convert(button.frame, to: self.view)
// Objective-C
CGRect frame = [imageView convertRect:button.frame toView:self.view];
回答by jbat100
There are four UIView
methods which can help you, converting CGPoints
and CGRects
from one UIView
coordinate reference to another:
有四种UIView
方法可以帮助您,将一个坐标参考转换CGPoints
并CGRects
从一个UIView
坐标参考转换为另一个:
– convertPoint:toView:
– convertPoint:fromView:
– convertRect:toView:
– convertRect:fromView:
so you can try
所以你可以试试
CGRect f = [imageView convertRect:button.frame toView:self.view];
or
或者
CGRect f = [self.view convertRect:button.frame fromView:imageView];
回答by Derek Soike
Swift 3
斯威夫特 3
You can convert the button's frame to the view's coordinate system with this:
您可以使用以下命令将按钮的框架转换为视图的坐标系:
self.view.convert(myButton.frame, from: myButton.superview)
self.view.convert(myButton.frame, from: myButton.superview)
Make sure to put your logic inside viewDidLayoutSubviews
and not viewDidLoad
. Geometry related operations should be performed after subviews are laid out, otherwise they may not work properly.
确保把你的逻辑放在里面viewDidLayoutSubviews
而不是viewDidLoad
. 布局子视图后应进行几何相关操作,否则可能无法正常工作。
class ViewController: UIViewController {
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myButton: UIButton!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let buttonFrame = self.view.convert(myButton.frame, from: myButton.superview)
}
}
You can just reference myButton.superview
instead of myImageView
when converting the frame.
您可以在转换帧时只引用myButton.superview
而不是引用myImageView
。
Here are more options for converting a CGPoint
or CGRect
.
这里有更多用于转换 aCGPoint
或 的选项CGRect
。
self.view.convert(point: CGPoint, from: UICoordinateSpace)
self.view.convert(point: CGPoint, from: UIView)
self.view.convert(rect: CGRect, from: UICoordinateSpace)
self.view.convert(rect: CGRect, from: UIView)
self.view.convert(point: CGPoint, to: UICoordinateSpace)
self.view.convert(point: CGPoint, to: UIView)
self.view.convert(rect: CGRect, to: UICoordinateSpace)
self.view.convert(rect: CGRect, to: UIView)
See the Apple Developer Docsfor more on converting a CGPoint
or CGRect
.
见苹果开发者文档的转换更多的CGPoint
还是CGRect
。
回答by Rasmus Styrk
Something like this? might be totally wrong, dint really thinkt it through ;p
像这样的东西?可能是完全错误的,真的想通了;p
CGRect frame = CGRectMake((self.view.frame.origin.x-imageview.frame.origin.x) +btn.frame.origin.x,
(self.view.frame.origin.y.imageview.frame.origin.y)+btn.frame.origin.y,
btn.frame.size.width,
btn.frame.size.height);
I don't know if theres any easier way.
不知道有没有更简单的方法。