ios - 导航栏右栏按钮项不可见

标签 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];
}

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

最佳答案

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

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

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

关于ios - 导航栏右栏按钮项不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856294/

相关文章:

ios - 在 iOS 上实现 "grand unified receipt"

ios - 如何在 iOS 中将字符串转换为 UTF-8 十进制

ios - 使用 Objective-C 属性 getter 实现 Swift 协议(protocol)

ios - 修改UINavigationBar标题字体和文字颜色的方法

ios - MonoTouch 中的自定义 UINavigationBar

ios - 使用导航 Controller 时滞后 UITableView 滚动

ios - swift plist 解码器全局 Spritekit

ios - iPad 应用中所有标签的模糊字体

ios - 使用 CocoaPods 防止 "There are multiple dependencies with different sources"

ios - 自动无限 collectionView - 闪烁