Kobe Kobe - 12 days ago 9x
Swift Question

How to get UIBarButtonItem center position

I am doing some custom view controller transitions and I want my transition to start from the center of the button that was pressed.


I can do:

myCustomTransition.startingPoint = buttonPressed.center

But if I want to use an
, how to actually tell my transition to start from the center of the button, because I don't see any
property on the

Any suggestions ?


If you just use UIBarButtonItem as navigationItem.leftBarButtonItem or navigationItem.rightBarButtonItem, you can't directly get there geometry property, because UIBarButtonItem doesn't inherit UIView, but you eventually can get geometry property by using tricky method.

This is the hierachy of a simple ViewController that has left and right UIBarButtonItem:

enter image description here

UINavigationButton is your target, get its rect and you can get the center what you need.

- (void)handleItemPressed {
UIView *targetView = nil;
for (UIView *subView in self.navigationController.navigationBar.subviews) {
    if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
        for (UILabel *label in subView.subviews) {
            if ([label isKindOfClass:[UILabel class]]) {
                if ([label.text isEqualToString:"right"]) {
                    targetView = subView;

CGRect rect = [targetView convertRect:targetView.frame toView:self.view];
CGPoint center_you_need = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));

If you use customeView,just convert customeView's frame to the point.