在iOS 7上首次启动后,LeftBarButtonItem未显示

时间:2013-10-16 14:28:42

标签: iphone ios ios7 uibarbuttonitem uinavigationitem

在我的主视图控制器中,我创建自定义leftBarButtonItem

- (void)viewDidLoad {
    [super viewDidLoad];

    RNObserveNotification(@"VTLoginSuccess", @selector(loginSuccess:));

    [self checkIfRoleChanged];


    self.navigationItem.leftBarButtonItem = [self carSelectBarItem];

}

- (UIBarButtonItem *)carSelectBarItem {    

    UIBarButtonItem *item = [self buttonItemWithText:@"     " target:self action:@selector(changeVehicleAction:)];
    VTCustomButton *button = (VTCustomButton *)[item customView];    

    UIImageView *carIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IconCar.png"]];
    [button addSubview:carIcon];    

    button.titleEdgeInsets = UIEdgeInsetsMake(0, 18.0, 0, 0);
    button.titleLabel.textAlignment = UITextAlignmentLeft;    

    CGRect frame = carIcon.frame;
    frame.origin.x += 8;
    frame.origin.y += 12;
    carIcon.frame = frame;

    self.currentVehicleButton = button;

    return item;

}

直到iOS 7问世。在iOS 7上首次启动应用程序时,该按钮根本不显示。但是,当再次构建和启动时,按钮就在那里并且正常工作。 知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我不确定为什么不起作用。尝试这样做:

- (UIBarButtonItem *)carSelectBarItem {
UIButton *button = [[UIButton alloc] init];

[button setImage:[UIImage imageNamed:@"IconCar.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(reloadUI)
    forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 30, 30)];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                           initWithCustomView:button];

return barButtonItem;
}

然后在你的viewDidLoad方法中:

[self.navigationItem setLeftBarButtonItem:[self carSelectBarItem]];

您可能需要从(0,0,30,30)

更改帧

答案 1 :(得分:0)

事实证明问题出在我的控制器堆栈中,修复了我的问题