Figure out UIBarButtonItem frame in window?

前端 未结 11 1921
臣服心动
臣服心动 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:09

    You can get it from the UINavigationBar view. The navigationBar is a UIView which has 2 or 3 custom subviews for the parts on the bar.

    If you know that the UIBarButtonItem is currently shown in the navbar on the right, you can get its frame from navbar's subviews array.

    First you need the navigationBar which you can get from the navigationController which you can get from the UIViewController. Then find the right most subview:

    UINavigationBar* navbar = curViewController.navigationController.navigationBar;
    UIView* rightView = nil;
    
    for (UIView* v in navbar.subviews) {
       if (rightView==nil) {
          rightView = v;
       } else if (v.frame.origin.x > rightView.frame.origin.x) {
          rightView = v;  // this view is further right
       }
    }
    // at this point rightView contains the right most subview of the navbar
    

    I haven't compiled this code so YMMV.

提交回复
热议问题