[英]Restore default navigation bar appearance
I'm making an iOS app for iPhone, and I'm using a navigation controller. 我正在为iPhone制作iOS应用程序,而我正在使用导航控制器。 At some point during the navigation, I'm adding a
UISegmentedControl
to a view controller, just under the navigation bar from the navigation controller. 在导航期间的某个时刻,我将
UISegmentedControl
添加到视图控制器,就在导航控制器的导航栏下方。 I'm inserting new background and shadow images in the navigation bar, to make the UISegmentedControl
appear as part of the navigation bar. 我在导航栏中插入新的背景和阴影图像,使
UISegmentedControl
显示为导航栏的一部分。 I do it like this: 我是这样做的:
// nav bar color image
let rect = CGRectMake(0, 0, view.frame.width, 0.5) // Used in navBar, size dosn't matter
UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
barBackgroundColor.setFill()
UIRectFill(rect)
let navBarBackground = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// setup navbar
navigationController!.navigationBar.setBackgroundImage(navBarBackground, forBarMetrics: .Default)
navigationController!.navigationBar.shadowImage = UIImage()
navigationController!.navigationBar.tintColor = UIColor.blackColor()
navigationController!.navigationBar.translucent = false
When I navigate away from that given view controller, the navigation bars background is still changed. 当我离开给定的视图控制器时,导航栏背景仍然会改变。
How can I restore the navigation bars appearance? 如何恢复导航栏的外观?
Or... 要么...
Is there another way embed the UISegmentedControl
into an expanded navigation bar? 是否有另一种方法将
UISegmentedControl
嵌入到扩展的导航栏中?
Image of navigation bar with custom background and Segmented Control below: 带有自定义背景和分段控制的导航栏图片如下:
When navigating back, the navigation bar cuntinues to be custom: 导航栏时,导航栏可以自定义:
EDIT: 编辑:
In a view controller before i change the background images, i try to safe the standart image: 在更改背景图像之前的视图控制器中,我尝试保护标准图像:
override func viewDidAppear(animated: Bool) {
if sharedVariables.standartNavBarBackgroundImage == nil {
let herp = navigationController!.navigationBar.backgroundImageForBarMetrics(.Default)
sharedVariables.standartNavBarBackgroundImage = herp
let derp = navigationController!.navigationBar.shadowImage
sharedVariables.standartNavBarShadowImage = derp
}
}
Both herp
and derp
are nil
after being set, dispite the navigationbar is visible at this momont. 设置后,
herp
和derp
都nil
,显示导航栏在此妈妈处可见。 How come? 怎么会?
只需将背景图像和阴影图像设置为nil
即可获得默认外观。
使用iOS 11. *和Swift 4,您需要将barTintColor
设置为nil
。
navigationController?.navigationBar.barTintColor = nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.