Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在另一个视图中获取视图的框架?_Ios_Objective C_Xcode_Uiimageview_Frame - Fatal编程技术网

Ios 如何在另一个视图中获取视图的框架?

Ios 如何在另一个视图中获取视图的框架?,ios,objective-c,xcode,uiimageview,frame,Ios,Objective C,Xcode,Uiimageview,Frame,我在self.view(主视图)中有一个UIImageView,里面有一个ui按钮。我想知道self.view中的ui按钮的框架是什么,而不是ui图像视图中的ui按钮的框架是什么?可能完全错了,力真的想透了;p CGRect frame = CGRectMake((self.view.frame.origin.x-imageview.frame.origin.x) +btn.frame.origin.x, (self.view.frame.or

我在
self.view
(主视图)中有一个
UIImageView
,里面有一个
ui按钮。我想知道
self.view
中的
ui按钮的框架是什么,而不是
ui图像视图中的
ui按钮的框架是什么?可能完全错了,力真的想透了;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);

我不知道是否有更简单的方法。

我想你正在寻找这种方法


有四种
UIView
方法可以帮助您将
CGPoints
CGRects
从一个
UIView
坐标参照转换为另一个:

– convertPoint:toView:
– convertPoint:fromView:
– convertRect:toView:
– convertRect:fromView:
所以你可以试试

CGRect f = [imageView convertRect:button.frame toView:self.view];


Swift 3

可以使用以下方法将按钮的框架转换为视图的坐标系:

self.view.convert(myButton.frame,from:myButton.superview)


确保将逻辑放入
viewdilayoutsubviews
中,而不是
viewdiload
。布局子视图后,应执行与几何图形相关的操作,否则这些操作可能无法正常工作

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)
    }
}
在转换帧时,您可以只参考
myButton.superview
而不是
myImageView


以下是转换
CGPoint
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)

有关转换
CGPoint
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)
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)