I
What I usually do is
1. Hide Navigation Bar
[self.navigationController setNavigationBarHidden:YES animated:NO];
2. Use image of size 640 x 88 & 320 x 44
UIImage *navigationImage = [UIImage imageNamed:@"navigationImage.png"];
3. Use this image in Imageview at frame CGRectMake(0,0,320,44)
UIImageView *navImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,44)];
navImageView.image = navigationImage;
[self.view addSubview:navImageView];
I have tried and got the below solution,
func addIconToNavigationBar(){
let logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "FlashScreenTitle")
imageView.image = image
logoContainer.addSubview(imageView)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: logoContainer)
}
In the above solution, I have Created a UIView and added ImageView inside that View. Then Created custom BarButtonItem, with my UIView and assigned it to the leftBarButtonItem.
Remember to call addIconToNavigationBar() in viewDidLoad() method
UIBarButtonItem *logoItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *logoItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItem target:self action:nil];
NSArray *actionButtonItems = @[logoItem1, logoItem2];
self.navigationItem.rightBarButtonItems = actionButtonItems;
Try it like this:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
Also make sure UIImage is not nil and it does have a valid image.
Insert the Bar Button Item
into your Viewcontroller
on left Side, and insert the image in Bar item
see the image
I think this is the problem with image rendering mode. Can you try the below code
UIImage *logo = [[UIImage imageNamed:@"logo"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:logo style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = item;
You can even try with your current code by setting the UIImageRenderingModeAlwaysOriginal
.