Problem visually:
视觉问题:
I have tried putting the image in the center of its own frame with no luck. I have also tried to center it with playing the x
of the CGRect with no luck either. I presume I can just put an empty icon with the same background as the navigation bar; however, I don't want to do it that way. I might have 2-3 icons on the right; then what?
我试过把这张照片放在自己画框的中间,但运气不佳。我也曾尝试过把它放在CGRect的x上,但运气不好。我假设我可以放一个空图标,背景和导航栏相同;但是,我不想那样做。右边可能有2-3个图标;然后呢?
let image = UIImage(named: "some_logo")!
let imageSize = CGSizeMake(60, 42)
let marginX: CGFloat = (self.navigationController!.navigationBar.frame.size.width / 2) - (imageSize.width / 2)
let imageView = UIImageView(frame: CGRect(x: marginX, y: 0, width: imageSize.width, height: imageSize.height))
imageView.image = image
imageView.contentMode = .ScaleAspectFit
self.navigationItem.titleView = imageView
I prefer swift but obj-c solutions are welcomed as well.
我更喜欢swift,但是object -c解决方案也受到欢迎。
Any pointers appreciated.
任何指针表示赞赏。
This app has nothing to do with KIA, it is just some logo I got off the google search, searching "some logo".
这款应用与起亚没有任何关系,它只是我在谷歌搜索中搜索到的某个logo。
5 个解决方案
#1
7
I have faced the same issue. Then i tried one code shown below.
我也遇到过同样的问题。然后我尝试了如下所示的代码。
override func viewDidAppear(animated: Bool) {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "googlePlus")
imageView.image = image
navigationItem.titleView = imageView
}
This Code working fine when i tested with Left & Right Bar Button.
当我用左右栏按钮测试时,这段代码运行良好。
But in my previous code there is no Right Bar Button.
但是在我之前的代码中没有正确的栏按钮。
So the image is moving towards right.
图像向右移动。
For solving this i created a Right Bar Button & change the Tint color to clear color.
为了解决这个问题,我创建了一个右栏按钮,并将颜色改为清晰的颜色。
So everything seems to be working fine. This is one Temporary Solution for your problem.
看来一切都很顺利。这是一个解决你问题的临时方案。
#2
4
The easiest way of doing this is in Interface Builder.
最简单的方法是在接口构建器中。
Simply drag a 'NavigationItem' from the object library and place it into your ViewController, then place a UIView where the title goes (ensure you set the background to 'clear') Then place a UIImageView into that view and set the image in the Attributes Inspector to your required image. Scale your UIImage accordingly and set your your constraints accordingly.
只需从对象库中拖拽一个" NavigationItem "并将其放入你的ViewController中,然后将一个UIView放在标题所在的位置(确保你将背景设置为" clear "),然后将一个UIImageView放到视图中,并将属性检查器中的图像设置为所需的图像。相应地缩放您的UIImage,并设置相应的约束。
#3
2
As question heading stated "Swift / Obj-C" so I am sharing code of Obj-C :
如问题标题为“Swift / object - c”,我将共享object - c代码:
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;
titleImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) - (100/2), 0, 100,self.navigationController.navigationBar.frame.size.height)];
//setting the image for UIImageView
titleImage.image = [UIImage imageNamed:@"someLogo"];
titleImage.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = titleImage;
#4
1
What about setting the center of your image equals to the navigationBar.center instead of setting a margin?
把图像的中心设置为navigationBar怎么样?居中而非留空?
//assuming we already have our navigationController
let myNicelLogoWidth = 100
let myNiceLogoHeight = 50
//start positioning your logo at 0.0, 0.0
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: myNicelLogoWidth, height: myNiceLogoHeight))
imageView.contentMode = .scaleAspectFit
imageView.center = navigationBar.center //the put your image at the center
let image = UIImage(named: "myNiceLogoImage")
imageView.image = image
navigationItem.titleView = imageView
#5
0
I once face with this problem, and finally i found out that the problem is the previous navigation bar title still located next to burger button, but it's invisible.
我曾经面对这个问题,最后我发现问题是之前的导航栏标题仍然位于汉堡按钮旁边,但它是不可见的。
Fast solution but not sure if it's the best is to change the previous navigation bar title to empty string before show the next view controller.
快速解决方案,但不确定它是否是最好的,是在显示下一个视图控制器之前将先前的导航栏标题更改为空字符串。
Hope it's help.
希望它的帮助。
#1
7
I have faced the same issue. Then i tried one code shown below.
我也遇到过同样的问题。然后我尝试了如下所示的代码。
override func viewDidAppear(animated: Bool) {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "googlePlus")
imageView.image = image
navigationItem.titleView = imageView
}
This Code working fine when i tested with Left & Right Bar Button.
当我用左右栏按钮测试时,这段代码运行良好。
But in my previous code there is no Right Bar Button.
但是在我之前的代码中没有正确的栏按钮。
So the image is moving towards right.
图像向右移动。
For solving this i created a Right Bar Button & change the Tint color to clear color.
为了解决这个问题,我创建了一个右栏按钮,并将颜色改为清晰的颜色。
So everything seems to be working fine. This is one Temporary Solution for your problem.
看来一切都很顺利。这是一个解决你问题的临时方案。
#2
4
The easiest way of doing this is in Interface Builder.
最简单的方法是在接口构建器中。
Simply drag a 'NavigationItem' from the object library and place it into your ViewController, then place a UIView where the title goes (ensure you set the background to 'clear') Then place a UIImageView into that view and set the image in the Attributes Inspector to your required image. Scale your UIImage accordingly and set your your constraints accordingly.
只需从对象库中拖拽一个" NavigationItem "并将其放入你的ViewController中,然后将一个UIView放在标题所在的位置(确保你将背景设置为" clear "),然后将一个UIImageView放到视图中,并将属性检查器中的图像设置为所需的图像。相应地缩放您的UIImage,并设置相应的约束。
#3
2
As question heading stated "Swift / Obj-C" so I am sharing code of Obj-C :
如问题标题为“Swift / object - c”,我将共享object - c代码:
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;
titleImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) - (100/2), 0, 100,self.navigationController.navigationBar.frame.size.height)];
//setting the image for UIImageView
titleImage.image = [UIImage imageNamed:@"someLogo"];
titleImage.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = titleImage;
#4
1
What about setting the center of your image equals to the navigationBar.center instead of setting a margin?
把图像的中心设置为navigationBar怎么样?居中而非留空?
//assuming we already have our navigationController
let myNicelLogoWidth = 100
let myNiceLogoHeight = 50
//start positioning your logo at 0.0, 0.0
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: myNicelLogoWidth, height: myNiceLogoHeight))
imageView.contentMode = .scaleAspectFit
imageView.center = navigationBar.center //the put your image at the center
let image = UIImage(named: "myNiceLogoImage")
imageView.image = image
navigationItem.titleView = imageView
#5
0
I once face with this problem, and finally i found out that the problem is the previous navigation bar title still located next to burger button, but it's invisible.
我曾经面对这个问题,最后我发现问题是之前的导航栏标题仍然位于汉堡按钮旁边,但它是不可见的。
Fast solution but not sure if it's the best is to change the previous navigation bar title to empty string before show the next view controller.
快速解决方案,但不确定它是否是最好的,是在显示下一个视图控制器之前将先前的导航栏标题更改为空字符串。
Hope it's help.
希望它的帮助。