How do I get the backBarButtonItem width?

I am trying to get the backBarButtonItem width:

UIBarButtonItem *leftBtn = self.navigationItem.backBarButtonItem;
UIView *view = [leftBtn valueForKey:@"view"];
CGFloat width;
if(view){
    width=view.frame.size.width;
}
NSLog(@"width %f", width);

      

but in the console: width 0.000000

Any ideas what I might be doing wrong? With self.navigationItem.rightBarButtonItem

it works.

+3


source to share


1 answer


UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CALayer *btnLayer = [addButton layer];
        [btnLayer setMasksToBounds:YES];
        [btnLayer setCornerRadius:1.0f];
    [addButton setFrame:CGRectMake(0, 0, 45, 30)];
    [addButton setBackgroundColor:[UIColor orangeColor]];
    [addButton setTitle:@"Title" forState:UIControlStateNormal];
    addButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [addButton addTarget:self action:@selector(Onclickevent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
    [self.navigationItem setRightBarButtonItem:barButtonItem];

      



0


source







All Articles