I am newbie for IOS. So I have tried to customize the UINavigationBar with out using navigation controller. I have able to change the tile and background color but I have tried to setting the background image programmatically its not working.whether its possible to set background image using story board itself ? I have tried may websites in that mostly used programmatically. so any possible way to set the background image using UIStoryboard ?
我是IOS的新手。所以我尝试使用导航控制器自定义UINavigationBar。我能够更改拼贴和背景颜色,但我尝试以编程方式设置背景图像不起作用。是否可以使用故事板本身设置背景图像?我尝试过可能以编程方式使用的网站。所以任何可能的方法来使用UIStoryboard设置背景图像?
2 个解决方案
#1
2
we can do like
我们可以这样做
and you get the output of
你得到的输出
Note
注意
if you load the Image from Assests use like UIImage(named: "xx")
如果您从Assests加载图像使用像UIImage(名称:“xx”)
if let img = UIImage(named: "xx")
{
self.topBar.setBackgroundImage( img , forBarMetrics: .Default)
}
else if you load the Image from resource bundle use like UIImage(named: "xx.extenson (.png )")
否则如果从资源包中加载图像使用像UIImage(名称:“xx.extenson(.png)”)
if let img = UIImage(named: "xx.jpg")
{
self.topBar.setBackgroundImage( img , forBarMetrics: .Default)
}
you can check with this function in apple douments
你可以在apple douments中查看这个功能
here I attached the sample Project link Download and check once
这里我附上了示例项目链接下载并检查一次
#2
0
You can use
您可以使用
[yourNavigationbar setBackgroundImage:[UIImage imageNamed: @"YourBackgroundImage.png"]
forBarMetrics:UIBarMetricsDefault];
For Swift
对于斯威夫特
yourNavigationbar.setBackgroundImage(UIImage(named: "YourBackgroundImage.png"),
forBarMetrics: .Default)
#1
2
we can do like
我们可以这样做
and you get the output of
你得到的输出
Note
注意
if you load the Image from Assests use like UIImage(named: "xx")
如果您从Assests加载图像使用像UIImage(名称:“xx”)
if let img = UIImage(named: "xx")
{
self.topBar.setBackgroundImage( img , forBarMetrics: .Default)
}
else if you load the Image from resource bundle use like UIImage(named: "xx.extenson (.png )")
否则如果从资源包中加载图像使用像UIImage(名称:“xx.extenson(.png)”)
if let img = UIImage(named: "xx.jpg")
{
self.topBar.setBackgroundImage( img , forBarMetrics: .Default)
}
you can check with this function in apple douments
你可以在apple douments中查看这个功能
here I attached the sample Project link Download and check once
这里我附上了示例项目链接下载并检查一次
#2
0
You can use
您可以使用
[yourNavigationbar setBackgroundImage:[UIImage imageNamed: @"YourBackgroundImage.png"]
forBarMetrics:UIBarMetricsDefault];
For Swift
对于斯威夫特
yourNavigationbar.setBackgroundImage(UIImage(named: "YourBackgroundImage.png"),
forBarMetrics: .Default)