【问题标题】:Restore default navigation bar appearance恢复默认导航栏外观
【发布时间】:2016-03-12 12:22:54
【问题描述】:

我正在为 iPhone 制作 iOS 应用,并且正在使用导航控制器。在导航期间的某个时刻,我将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 嵌入到展开的导航栏中吗?

具有自定义背景和分段控件的导航栏图像如下:

当导航返回时,导航栏是自定义的:

编辑:

在更改背景图像之前,在视图控制器中,我尝试保护标准图像:

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
    }
}

herpderp 设置后都是nil,尽管在这个momont 上可以看到导航栏。怎么会?

【问题讨论】:

  • 显示您正在寻找的最终结果的图片以及如果您不更改图片会是什么样子
  • 图片添加..最终结果应该是标准的导航栏外观..

标签: ios swift uinavigationcontroller uinavigationbar


【解决方案1】:

您应该能够通过将背景图像和阴影图像设置为nil 来恢复默认外观。

【讨论】:

    【解决方案2】:

    使用 iOS 11.* 和 Swift 4,您需要将 barTintColor 设置为 nil

    navigationController?.navigationBar.barTintColor = nil
    

    【讨论】:

    • 如果之前没有更改,则不会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多