1

在我的主视图控制器中,我创建了自定义 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 上,当应用程序首次启动时,按钮根本不显示。但是,当构建并再次启动时,按钮就在那里并且工作正常。知道如何解决这个问题吗?

4

2 回答 2

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)

于 2013-10-16T15:30:24.287 回答
0

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

于 2013-10-31T13:50:39.117 回答