1

I need to get the CGPoint of a UIBarButtonItem. I know I can get them of a normal UIButton by doing this:

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

But this does not seem to work if I alter it for a UIBarButtonItem. The reason I need the program to return these values to me, is because otherwise with the iPhone 5, the 4S and before screen I need to set up 3 possible positions when I include the landscape version, which I will do if there is no easier way to achieve this.

jwknz
  • 6,598
  • 16
  • 72
  • 115
  • What happens in different screens (in landscape) if you don't do anything? Does it not adjust automatically? – rdelmar Jan 27 '13 at 04:22
  • 1
    UIBarButtonItem do not have a frame property - so this code won't do the job – jwknz Jan 27 '13 at 05:38
  • 1
    Er, could you clearly explain why you need this? – tc. Jan 27 '13 at 06:00
  • I have a pop over that is activated when the button is pressed and I need to go where the UIBarButton is located, which can be in 3 places when you look at the coordinates. The example above avoids a lot of code, but I can't get it to work just as simple for the button I am using. – jwknz Jan 27 '13 at 06:04

1 Answers1

3

I found this answer here.

As was pointed out, UIBarButtonItem does not extend UIView thus it has no frame property. The work around offered in the link is a private API and is typically not recommended.

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

There is another solution offered in the link too.

Community
  • 1
  • 1
Firo
  • 15,448
  • 3
  • 54
  • 74
  • Cool thanks for that - I have got it sussed, but it is just managed with if statements based on the type of device that uses the app. – jwknz Jan 27 '13 at 06:08