Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add title and image to navigationbar

I need so set the name of the UIViewController and an image to the navigationBar. So far I am able to display the image, but the title is of course missing.

    // show image
    UIImage *image = [UIImage imageNamed: @"bar_icon.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    imageView.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.titleView = imageView;
    [imageView release];

Thanks for your help,
BR, doonot

like image 382
nimrod Avatar asked Dec 16 '22 16:12

nimrod


2 Answers

    // show image
    UIImage *image = [UIImage imageNamed: @"bar_icon.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    imageView.frame = CGRectMake(70, 0, 40, 40);

    // label
    UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
    tmpTitleLabel.text = @"Mannschaft";
    tmpTitleLabel.backgroundColor = [UIColor clearColor];
    tmpTitleLabel.textColor = [UIColor whiteColor];

    CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
    UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
    [newView addSubview:imageView];
    [newView addSubview:tmpTitleLabel];
    self.navigationItem.titleView = newView;

    [imageView release];
    [tmpTitleLabel release];
like image 168
nimrod Avatar answered Jan 02 '23 20:01

nimrod


Create a UIView with 2 subview: a UIImageView and a UILabel. Set the titleView to the UIView.

like image 28
unexpectedvalue Avatar answered Jan 02 '23 20:01

unexpectedvalue