4

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

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
nimrod
  • 5,595
  • 29
  • 85
  • 149

2 Answers2

8
    // 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];
nimrod
  • 5,595
  • 29
  • 85
  • 149
  • is there a way to get a Rect based on the image you give UIImageView? why do you have to specify a Rect if, say, you just want the rect to be the size of the image? – topwik Jun 06 '13 at 20:23
4

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

unexpectedvalue
  • 6,079
  • 3
  • 38
  • 62