Ios 如何在导航栏中放置UIImage,使其成为徽标?
我想在Ios 如何在导航栏中放置UIImage,使其成为徽标?,ios,objective-c,uiimage,uinavigationcontroller,Ios,Objective C,Uiimage,Uinavigationcontroller,我想在导航栏的左侧放置一个徽标/图像。我在代码中尝试了这一点,但工作不正常 UIImage *logo = [UIImage imageNamed:@"logo.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage: logo]; imageView.frame = CGRectMake(2, 2, 40, 40); self.navigationItem.titleView = [[UIImageView allo
导航栏的左侧放置一个徽标/图像。我在代码中尝试了这一点,但工作不正常
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];
我尝试先放置视图,然后将图像放置为UIImageView,但对齐效果不好。此外,在运行代码时,视图显示为空白。请尝试以下操作:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
还要确保UIImage不是nil,并且它确实有一个有效的映像 像这样试试:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
还要确保UIImage不是nil,并且它确实有一个有效的映像 将条形按钮项目
插入左侧的视图控制器
,然后将图像插入条形项目
查看图像
将条形按钮项插入左侧的视图控制器
,然后将图像插入条形按钮项
查看图像
我通常做的是
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];
我通常做的是
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];
我在上一个项目中遇到了同样的问题,你可以创建一个新的logo.png,它的尺寸等于500*88像素,让你的logo位于它的左侧。填充了png其余部分的透明颜色
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logoName_500.png"]];
[titleImageView setFrame:CGRectMake(0, 0, 250, 44)];
self.navigationItem.titleView = titleImageView;
希望这有帮助。我在上一个项目中遇到了同样的问题,你可以创建一个新的logo.png,它的尺寸等于500*88像素,使你的logo位于它的左侧。填充了png其余部分的透明颜色
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logoName_500.png"]];
[titleImageView setFrame:CGRectMake(0, 0, 250, 44)];
self.navigationItem.titleView = titleImageView;
希望这有帮助。我认为这是图像渲染模式的问题。你能试试下面的代码吗
UIImage *logo = [[UIImage imageNamed:@"logo"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:logo style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = item;
您甚至可以通过设置UIImageRenderingModeAlwaysOriginal
来尝试使用当前代码。我认为这就是图像渲染模式的问题所在。你能试试下面的代码吗
UIImage *logo = [[UIImage imageNamed:@"logo"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:logo style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = item;
@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;
您甚至可以通过设置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;
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)
}
在上面的解决方案中,我创建了一个UIView,并在该视图中添加了ImageView。然后使用my UIView创建自定义巴布托体,并将其指定给Left巴布托体
记住在viewDidLoad()方法中调用AddicontaVigationBar()我已经尝试并得到了以下解决方案
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)
}
在上面的解决方案中,我创建了一个UIView,并在该视图中添加了ImageView。然后使用my UIView创建自定义巴布托体,并将其指定给Left巴布托体
记住在viewDidLoad()方法中调用addIconToNavigationBar()您使用过故事板是的,我在故事板ios7中工作。使用以下代码代替最后一行:self.navigationItem.titleView=imageView;我仍然对答案不满意吗?然后我会回答。@Nirav:发布你的答案。。。即使我是在科威特作为iOS开发者工作..你使用了storyboard是的,我在storyboard,ios7中工作。使用以下代码代替最后一行:self.navigationItem.titleView=imageView;我仍然对答案不满意吗?然后我会回答。@Nirav:发布你的答案。。。即使我在科威特作为iOS开发者工作,你需要任何修改吗它工作得很好。。但它显示为栏按钮项,即整个图像显示为蓝色。在您的问题中,将图像设置在导航栏的左侧,这是我给出的,你需要在程序中添加图像吗?你需要在没有导航栏的情况下添加。调整色调和插图到你喜欢的颜色/大小。你需要任何修改吗?效果很好。。但它显示为栏按钮项,即整个图像显示为蓝色。在您的问题中,将图像设置在导航栏的左侧,这是我给出的,你需要在程序中添加图像吗?你需要在没有导航栏的情况下添加。调整色调和插图到你喜欢的颜色/大小。我使用导航控制器,需要显示导航栏,导航栏上有bg图像,我需要将我的徽标放在左边。我无法上传快照。@yazh:你能发布代码说明你是如何拥有这些图片的吗导航栏的背景图像?我会说在dropbox上发布您的示例代码…我使用导航控制器,需要显示导航栏,导航栏上有bg图像,我需要将我的徽标放在左侧。我无法上载快照。@yazh:您可以发布代码,说明您如何获得导航栏的背景图像吗?我想说把你的示例代码放在dropbox上…这和你的代码有点不同。我确实看了。你检查过UIImage不是零吗?它和你的代码有点不同。我确实看了。你检查过UIImage不是零吗?