How to place a UIImage in Navigationbar such that its a logo?

心已入冬 提交于 2019-11-29 17:27:37

Insert the Bar Button Item into your Viewcontroller on left Side, and insert the image in Bar item see the image

lixiang

I had the same problem in my last project, you can create a new logo.png which dimension is equal to 500*88 pixels, make you logo in the left side of it. filled with transparent color from the rest of the png.

UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logoName_500.png"]];
[titleImageView setFrame:CGRectMake(0, 0, 250, 44)];
self.navigationItem.titleView = titleImageView;

hope this helpful.

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.

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 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.

@property(nonatomic,strong) UIView *titleImageView;


- (UIView *)titleImageView {
if (!_titleImageView) {
     CGFloat width = self.view.frame.size.width;
    _titleImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
    [self.titleLabelView setBackgroundColor:[UIColor clearColor]];
    UIImage *img = [UIImage imageNamed:@"myImg"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    CGRect imageViewRect = imgView.frame;
    /*
    //adjust image position if you need
    imageViewRect.x = 10;
    imageViewRect.y = 3;
    imageView.frame = imageViewRect;
    */
    [_titleLabelView addSubview:imgView];
 }
return _titleLabelView;
}


self.navigationItem.titleView = self.titleLabelView;
Sumit Chauhan
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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!