Figure out UIBarButtonItem frame in window?

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

    This is not the best solution and from some point of view it's not right solution and we can't do like follow because we access to object inside UIBarBattonItem implicitly, but you can try to do something like:

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [button setImage:[UIImage imageNamed:@"Menu_Icon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didPressitem) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    
    CGPoint point = [self.view convertPoint:button.center fromView:(UIView *)self.navigationItem.rightBarButtonItem];
    //this is like view because we use UIButton like "base" obj for 
    //UIBarButtonItem, but u should note that UIBarButtonItem base class
    //is NSObject class not UIView class, for hiding warning we implicity
    //cast UIBarButtonItem created with UIButton to UIView
    NSLog(@"point %@", NSStringFromCGPoint(point));
    

    as result i got next:

    point {289, 22}

提交回复
热议问题