简体   繁体   中英

Navigation bar right bar button item is not visible

- (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];
}

My code is given above: The problem, I am facing is that navigation right bar button items are not visible first time when I navigate to home. When I navigate to some other page and comes back then its visible. The first method is used to create right navigation bar button items.

From Apple doc on rightBarButtonItems, you can see that most likely your custom view is too wide and your button is not showing because it does not fit. Test making it narrower and see if it appears?

Discussion This array can contain 0 or more bar button items to display on the right side of the navigation bar. Items are displayed right-to-left in the same order as they appear in the array. Thus, the first item in the array is the rightmost item and other items are added to the left of the previous item.

If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM