【问题标题】:how do I get the x/y co-ordinates of a UIView origin used within a UINavigationController?如何获取 UINavigationController 中使用的 UIView 原点的 x/y 坐标?
【发布时间】:2011-09-14 23:01:56
【问题描述】:

如何获取 UINavigationController 中使用的 UIView 原点的 x/y 坐标?

那是我有一个 UINavigationController iPhone 应用程序设置。我将一个名为 CustomUIView 的新视图推送到堆栈上。如何才能将 CustomView 视图的绝对原点(x/y 坐标)相对于整个 iPhone 屏幕?也就是说,它不应该是 0,0,因为它应该考虑到顶部的导航栏等。

我注意到我得到: * 对于导航控制器:frame = (0 0; 320 480) * 对于 customViewController.view:frame = (0 0; 320 416) * 对于 customView 本身:frame = (0 0; 320 396)

所以问题是,当我在 customView 代码本身中,并且我得到它的框架时,它给了我 0,0 的原点......

背景:我正在尝试将图像定位在 customView 上的某个位置的中心,但它似乎是偏移的,我猜是因为当我尝试设置它相对于 customView 的位置时,也许我应该相对于 NavController 整体框架进行设置。我希望图像居中的位置基于在屏幕上获得触摸事件:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
    for (UITouch *touch in touches){
        [self dispatchTouchEvent:[touch locationInView:self]];
    }
}

PS。也许另一个有用的答案是,如果您确实从 UITouch 事件中获取坐标,然后想要使用它来将图像居中在这些坐标上,那么您将在基于 UINavigationController 的 iPhone 中的 UIview 子视图中使用的代码是什么应用程序,这将使图像正确居中....

【问题讨论】:

    标签: iphone ios uiview uinavigationcontroller touchesmoved


    【解决方案1】:

    CGPoint convertedPoint = [self.view.superview convertPoint:self.view.frame.origin toView:nil];

    请参阅 convertPoint:toView 的文档: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/convertPoint:toView:

    【讨论】:

      猜你喜欢
      • 2022-11-13
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多