【问题标题】:ios - Get coordinates or CGPoint of a UIBarButtonItemios - 获取 UIBarButtonItem 的坐标或 CGPoint
【发布时间】:2013-01-27 03:20:38
【问题描述】:

我需要获取 UIBarButtonItem 的 CGPoint。我知道我可以通过这样做获得它们的普通 UIButton:

- (IBAction)buttonTapped:(UIButton *)sender
  {
    CGPoint coordinates = sender.frame.origin;
  }

但是,如果我为 UIBarButtonItem 更改它,这似乎不起作用。 我需要程序将这些值返回给我的原因是,否则在 iPhone 5、4S 和屏幕前,当我包含横向版本时,我需要设置 3 个可能的位置,如果没有更简单的方法,我会这样做实现这一目标的方法。

【问题讨论】:

  • 如果你什么都不做,在不同的屏幕(横向)会发生什么?它不会自动调整吗?
  • UIBarButtonItem 没有 frame 属性 - 所以这段代码无法完成这项工作
  • 呃,你能清楚地解释为什么你需要这个吗?
  • 我有一个弹出窗口,在按下按钮时会激活,我需要转到 UIBarButton 所在的位置,当您查看坐标时,它可能位于 3 个位置。上面的例子避免了很多代码,但我不能让它像我使用的按钮一样简单。

标签: ios uibutton uibarbuttonitem cgpoint


【解决方案1】:

我找到了这个答案here

正如所指出的,UIBarButtonItem 没有扩展 UIView,因此它没有 frame 属性。链接中提供的解决方法是私有 API,通常不推荐使用。

UIView* barView = sender.view;
CGRect barFrame = [barView convertRect:barView.bounds toView:nil];

链接中还提供了另一种解决方案。

【讨论】:

  • 非常感谢 - 我已经猜到了,但它只是根据使用该应用程序的设备类型使用 if 语句进行管理。
猜你喜欢
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多