简体   繁体   中英

change navigation bar title's color

I know how to change the color of my navigation bar title but when I write this code:

UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor yellowColor];
label.backgroundColor=[UIColor clearColor]; 
self.navigationItem.titleView = label;
label.text=@"Title"; //CUSTOM TITLE
[label sizeToFit];

I want my navigation bar to show the cell's title, not custom title.

Here is my table view code:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ghazalList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [ghazalList objectAtIndex:row];
    return cell; 
} 

Based on your explanation, try

UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor yellowColor];
label.backgroundColor = [UIColor clearColor]; 
label.text = self.navigationItem.title;
self.navigationItem.titleView = label;
[label sizeToFit];

@Adam :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!anotherViewController) {
    anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
    anotherViewController.navigationItem.title=[ghazalList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:anotherViewController animated:YES];
    [anotherViewController loadGhazal:indexPath.row];

}else {
    [anotherViewController loadGhazal:indexPath.row];
    anotherViewController.navigationItem.title=[ghazalList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:anotherViewController animated:YES];  
}

}

i creat a poem application so , i have 495 cell and 495 HTML file [poems] . each cell [POEM 1 to 495] has a special pooem . everything works fine but , this damn title color drive me crazy ... i put your code the color had changed but the title changed to Poem 5 !

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