Figure out UIBarButtonItem frame in window?

前端 未结 11 1925
臣服心动
臣服心动 2020-11-28 02:58

UIBarButtonItem does not extend UIView, so there is nothing like a frame property.

But is there any way I can get what is it\'s CGRec

11条回答
  •  悲哀的现实
    2020-11-28 03:23

    This is the implementation I use for my WEPopover project: (https://github.com/werner77/WEPopover):

    @implementation UIBarButtonItem(WEPopover)
    
    - (CGRect)frameInView:(UIView *)v {
    
        UIView *theView = self.customView;
        if (!theView.superview && [self respondsToSelector:@selector(view)]) {
            theView = [self performSelector:@selector(view)];
        }
    
        UIView *parentView = theView.superview;
        NSArray *subviews = parentView.subviews;
    
        NSUInteger indexOfView = [subviews indexOfObject:theView];
        NSUInteger subviewCount = subviews.count;
    
        if (subviewCount > 0 && indexOfView != NSNotFound) {
            UIView *button = [parentView.subviews objectAtIndex:indexOfView];
            return [button convertRect:button.bounds toView:v];
        } else {
            return CGRectZero;
        }
    }
    @end
    

提交回复
热议问题