【问题标题】:Navigation bar right bar button item is not visible导航栏右栏按钮项不可见
【发布时间】:2014-04-04 07:28:34
【问题描述】:
- (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];
}

上面给出了我的代码:我面临的问题是,当我第一次导航到主页时,导航右栏按钮项不可见。当我导航到其他页面并返回时,它是可见的。第一种方法用于创建右导航栏按钮项。

【问题讨论】:

  • 检查self.navigationTableViewController.navigationItem是否为nil。
  • @KudoCC 谢谢伙计,但它不是 nil,如果它是 nil,按钮不应该出现在从视图返回时。
  • 你最好在self.centreViewControllerviewWillAppear方法中配置导航栏或尝试将[self setRightNavigationBarViewForUser];这一行移到navigateToHome方法的底部(确保添加弹出动作后的项目,也许它应该进入completionHandler)

标签: ios objective-c uinavigationbar uinavigationitem


【解决方案1】:

从关于 rightBarButtonItems 的 Apple 文档中,您可以看到您的自定义视图很可能太宽,并且您的按钮没有显示,因为它不适合。试试把它变窄看看有没有出现?

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

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

【讨论】:

  • 当我从家导航到任何其他地方并返回时,它会出现,因为我想要的视图是完美的。但是当我第一次导航到家时它没有出现。
  • 可能是因为您导航回家后您正在更改左侧按钮栏,并且还有更多空间?正如我所说,尝试暂时缩小项目以确认这是宽度问题。