UIBarButtonItem: How can I find its frame?

前端 未结 16 904
忘掉有多难
忘掉有多难 2020-12-12 23:17

I have a button in a toolbar. How can I grab its frame? Do UIBarButtonItems not have a frame property?

16条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 00:13

    Here's the way to do it:

    -(CGRect)findFrameOfBarButtonItem{
        for (UIView *view in self.navigationController.navigationBar.subviews)
        {
           if ([view isKindOfClass:NSClassFromString(@"_UINavigationBarContentView")])
           {
             UIView * barView = [self.navigationItem.rightBarButtonItem valueForKey:@"view"];
             CGRect barFrame = barView.frame;
             CGRect viewFrame = [barView convertRect:barFrame toView:view];
             return viewFrame;
           }
        }
    
        return CGRectZero;
    }
    

提交回复
热议问题