How to place a UIImage in Navigationbar such that

2019-01-29 08:15发布

enter image description hereenter image description hereI would like to place a logo/image in the left side of NavigationBar. I try this in codes, but it is not worked properly.

UIImage *logo = [UIImage imageNamed:@"logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: logo];
imageView.frame = CGRectMake(2, 2, 40, 40);
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logo];

I try with view to be placed first then place image as UIImageView but alignment is not good. And moreover view appears to be white space while run the code..

7条回答
叼着烟拽天下
2楼-- · 2019-01-29 08:50

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];
查看更多
女痞
3楼-- · 2019-01-29 08:51

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.

查看更多
Root(大扎)
4楼-- · 2019-01-29 08:54
@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;
查看更多
倾城 Initia
5楼-- · 2019-01-29 09:01

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.

查看更多
唯我独甜
6楼-- · 2019-01-29 09:08

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

enter image description here

查看更多
冷血范
7楼-- · 2019-01-29 09:11

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.

查看更多
登录 后发表回答