【问题标题】:How to get frame of UIView Nested in UIView's - ObjectiveC如何获取嵌套在 UIView 中的 UIView 框架 - Objective C
【发布时间】:2016-05-22 04:53:50
【问题描述】:

我有一个应用程序,其中有很多 UIView's 嵌套在一起。

我想找到一些UIView's相对于Controller的框架。

我已经设置了一个例子:

我可以找到白色 UIView w.r.t 到 Controller 的框架,但我如何才能找到 RedView wrt 到控制器的框架。

查找White View的Frame的代码:

      - (void)viewDidAppear:(BOOL)animated
      {
           NSLog(@"--OuterFrame---%@",NSStringFromCGRect(self.grayView.frame));
           NSLog(@"--InnerFrame---%@",NSStringFromCGRect(self.whiteView.frame));

           NSLog(@"---Frame With resepect to self.view --%@",NSStringFromCGRect([self.grayView convertRect:self.whiteView.frame toView:self.view]));
          }

如果他们是红色视图中的另一个视图。

【问题讨论】:

  • self.view.convertRect(redView.frame, fromView:self.whiteView)?

标签: ios objective-c iphone xcode uiview


【解决方案1】:

您的总体思路是正确的。但是如果你使用内部视图的bounds,并使用self.view进行转换会更容易:

// assumes innerView is a view somewhere underneath self.view
CGRect innerViewRectRelativeToController = 
    [self.view convertRect:innerView.bounds fromView:innerView];

这样你只需要指定要转换的视图和要转换到的视图。您无需考虑两者之间的视图。

【讨论】:

  • 谢谢...它工作得很好...我尝试这个来找到 redView 的框架并且工作得很好。我想问的一个问题......这种方法是否独立于嵌套......意味着我的视图在 UIView 中更深 10 级,这可行吗?
  • 是的,我正在玩它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 2010-12-16
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 2015-01-07
相关资源
最近更新 更多