Ios 恢复默认导航栏外观
我正在为iPhone制作一个iOS应用程序,我正在使用一个导航控制器。在导航过程中的某个时刻,我将向视图控制器添加一个Ios 恢复默认导航栏外观,ios,swift,uinavigationcontroller,uinavigationbar,Ios,Swift,Uinavigationcontroller,Uinavigationbar,我正在为iPhone制作一个iOS应用程序,我正在使用一个导航控制器。在导航过程中的某个时刻,我将向视图控制器添加一个UISegmentedControl,就在导航控制器的导航栏下。我正在导航栏中插入新的背景和阴影图像,以使UISegmentedControl显示为导航栏的一部分。我是这样做的: // nav bar color image let rect = CGRectMake(0, 0, view.frame.width, 0.5) // Used in
UISegmentedControl
,就在导航控制器的导航栏下。我正在导航栏中插入新的背景和阴影图像,以使UISegmentedControl
显示为导航栏的一部分。我是这样做的:
// 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
当我离开给定的视图控制器时,导航栏的背景仍然会改变
如何恢复导航栏的外观?
或者
是否有其他方法将UISegmentedControl
嵌入到扩展的导航栏中?
具有自定义背景和分段控件的导航栏图像如下:
向后导航时,导航栏C将继续自定义:
编辑:
在视图控制器中,在更改背景图像之前,我尝试保护standart图像:
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
}
}
herp
和derp
都是nil
设置后,尽管导航栏在此位置可见。为什么呢?在iOS 11.*和Swift 4中,只要将背景图像和阴影图像设置为nil
,您就可以恢复默认外观。您需要将BartinColor
设置为nil
navigationController?.navigationBar.barTintColor = nil
显示您正在查找的最终结果的图片,以及如果您不更改添加的图像,它的外观。最终结果应为标准导航栏外观。如果以前未更改,则不显示。