如何获得backBarButtonItem宽度?

时间:2014-12-29 01:09:59

标签: ios objective-c uikit

我尝试获取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);

但在控制台中:width 0.000000

任何想法我可能做错了什么?使用self.navigationItem.rightBarButtonItem可以正常工作。

1 个答案:

答案 0 :(得分:0)

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];