如何将一个视图的框架放入另一个视图中?

发布于 2024-12-14 21:50:54 字数 153 浏览 2 评论 0原文

我在 self.view (主视图)中有一个 UIImageView ,里面有一个 UIButton 。我想知道 self.view 中的 UIButton 的框架是什么,而不是 UIImageView 中的框架。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七月上 2024-12-21 21:50:54

我猜你正在寻找这个方法

–convertRect:toView:

// Swift
let frame = imageView.convert(button.frame, to: self.view)

// Objective-C
CGRect frame = [imageView convertRect:button.frame toView:self.view];

I guess you are looking for this method

– convertRect:toView:

// Swift
let frame = imageView.convert(button.frame, to: self.view)

// Objective-C
CGRect frame = [imageView convertRect:button.frame toView:self.view];
回忆追雨的时光 2024-12-21 21:50:54

有四种 UIView 方法可以帮助您将 CGPointsCGRects 从一个 UIView 坐标引用转换为另一个:

– convertPoint:toView:
– convertPoint:fromView:
– convertRect:toView:
– convertRect:fromView:

所以你可以尝试

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

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

There are four UIView methods which can help you, converting CGPoints and CGRects from one UIView coordinate reference to another:

– 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];
谈情不如逗狗 2024-12-21 21:50:54

Swift 3

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

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


确保将您的逻辑位于 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)
    }
}

转换框架时,您可以只引用 myButton.superview 而不是 myImageView


以下是转换 CGPointCGRect 的更多选项。

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)

请参阅 Apple 开发人员文档 有关转换 CGPointCGRect 的更多信息。

Swift 3

You can convert the button's frame to the view's coordinate system with this:

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.

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.


Here are more options for converting a CGPoint or 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 Docs for more on converting a CGPoint or CGRect.

初心未许 2024-12-21 21:50:54

像这样的东西吗?可能是完全错误的,我真的想通了;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);

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

Something like this? might be totally wrong, dint really thinkt it through ;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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文