问题

I 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..
回答1:
Insert the Bar Button Item into your Viewcontroller on left Side, and insert the image in Bar item see the image
回答2:
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.
回答3:
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.
回答4:
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];
回答5:
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.
回答6:
@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;
回答7:
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;
回答8:
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
来源:https://stackoverflow.com/questions/23881773/how-to-place-a-uiimage-in-navigationbar-such-that-its-a-logo