导航栏右栏按钮项目不可见

时间:2014-04-04 07:28:34

标签: ios objective-c uinavigationbar uinavigationitem

- (void)setRightNavigationBarViewForUser {
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                               target:nil action:nil];
    spacer.width = 760;
    NSString *title = [VimondStore sessionManager].userName;

    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 144, 44)];
    tempView.backgroundColor = [UIColor clearColor];
    UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 0, 44, 44)];
    tempImageView.image = [UIImage imageNamed:@"user.png"];

    UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 80, 44)];
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.text = title;
    tempLabel.font = [UIFont boldSystemFontOfSize:14.0];
    tempLabel.textColor = [UIColor whiteColor];
    tempLabel.textAlignment = NSTextAlignmentLeft;
    tempLabel.adjustsFontSizeToFitWidth = YES;
    tempLabel.minimumScaleFactor = 0.8;
    [tempView addSubview:tempImageView];
    [tempView addSubview:tempLabel];
    UIBarButtonItem *userView = [[UIBarButtonItem alloc]initWithCustomView:tempView];
    NSArray *items = @[spacer ,userView];
    self.navigationTableViewController.navigationItem.rightBarButtonItems = items;
}

- (void)navigateToHome {
    [self setRightNavigationBarViewForUser];
    self.loginViewController = nil;
    [self showCenterPanelAnimated:YES];
    [self setLeftBarButtonForDrawerTitle];
    NSAssert([self.centreViewController isKindOfClass:[GGBaseViewController class]], @"Must be of GGBaseViewController class");
    [GenreNavigator navigateToRoot:(GGBaseViewController*)self.centreViewController completionHandler:nil];
}

我的代码如上所示:我面临的问题是,当我导航到家时,导航右侧栏按钮项目不可见。当我导航到其他页面并返回时它可见。第一种方法用于创建右侧导航栏按钮项。

1 个答案:

答案 0 :(得分:1)

从rightBarButtonItems上的Apple doc,你可以看到你的自定义视图很可能太宽而你的按钮没有显示,因为它不合适。测试它是否更窄,看看它是否出现?

讨论 此数组可以包含0个或更多条形按钮项目,以显示在导航栏的右侧。项目从右到左显示的顺序与它们在数组中显示的顺序相同。因此,数组中的第一项是最右边的项,其他项添加到上一项的左侧。

如果没有足够的空间显示数组中的所有项目,则不显示与标题视图(如果存在)或栏左侧按钮重叠的项目。