1

I am attempting to resize the image that I placed in the navigationItem.titleView of my TableViewController. I used the following code in my TableViewController viewDidLoad to accomplish this:

UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
iv.contentMode = UIViewContentModeScaleAspectFit;
UIImage* image = [UIImage imageNamed:@"logo.png"];
[iv setImage:image];
self.navigationItem.titleView = iv;

However, the image does not seem to be affected by the frame of 40 x 40 that I set. The result looks like this:

navigation bar with large logo

The image is clearly exceeding the 40 x 40 limit that I specified. How can I get the image to not take up all the available space in the navigation bar? I want the logo to be smaller and have a bit of padding on the top and bottom.

Nathan Van Dyken
  • 342
  • 1
  • 8
  • 21

1 Answers1

2

You should create an UIView, add your logo on it and after that set self.navigationItem.titleView with this view. You can check my code below.

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:titleView.frame];
titleImageView.image = [UIImage imageNamed:@"logo.png"];;
titleImageView.contentMode = UIViewContentModeScaleAspectFit;

[titleView addSubview:titleImageView];

self.navigationItem.titleView = titleView;
trungduc
  • 11,926
  • 4
  • 31
  • 55