2016-04-30 48 views
1

我正在开发一个应用程序,其中应用程序的主页具有隐藏细线的白色导航栏,其余页面具有绿色导航栏。到目前为止,我所做的是将主页导航栏的代码放在自己的.swift文件中,但是当我通过主页上的按钮导航到其他页面时(与菜单栏相反) )导航栏仍然设置为白色。我认为这是因为当通过按钮访问时,删除细线的代码会传递到其他页面。如何在Swift中为不同页面撤消发条删除?

这是我的网页应该非常像: Homepage & Module

这实际上是模块页面的样子:Module Page Now

这是我使用的代码 - 不知道是否有人能帮助我弄清楚如何反转代码或者是否有其他解决方案。

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    // Set navigation bar tint/background colour 
    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() 

    // Set Navigation bar Title colour 
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()] 

    //Set navigation bar Back button tint colour 
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor() 

    //Get Rid of 1px hairline 
    self.navigationController?.navigationBar.setBackgroundImage(
     UIImage(), 
     forBarPosition: .Any, 
     barMetrics: .Default) 

    self.navigationController?.navigationBar.shadowImage = UIImage() 

} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 

    // Set navigation bar tint/background colour 
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 0/255, green: 127/255, blue: 106/255, alpha: 1) 

    // Set Navigation bar Title colour 
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] 

    //Set navigation bar Back button tint colour 
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() 

    //Reverse get Rid of 1px hairline code 
    /*self.navigationController?.navigationBar.setBackgroundImage(
     UIImage(), 
     forBarPosition: .Any, 
     barMetrics: .Default) 

    self.navigationController?.navigationBar.shadowImage = UIImage()*/ 
} 
+0

你是什么意思为“发际线”? –

+0

@AlessandroOrnano与导航栏底部相邻的1px线 – zenpain

回答

0

能够通过反转viewWillDisappear方法中的代码来解决此问题。

//Reverse get Rid of 1px hairline code 
self.navigationController?.navigationBar.setBackgroundImage(nil, forBarMetrics: .Default) 
0

你可以用一般的方法(称为例如Utils.swift)的自定义快捷文件,并插入你的函数,然后调用它在未来的控制器:

import Foundation 
import UIKit 

//MARK: - General methods: 
    func customizeNavigationController(navigationController:UINavigationController) { 
      navigationController.navigationBar.backgroundColor = UIColor.greenColor() 
      navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) 
      navigationController.navigationBar.barTintColor = UIColor.greenColor() 
      navigationController.navigationBar.tintColor = UIColor.blackColor() 
      navigationController.navigationBar.barStyle = UIBarStyle.Black 
      navigationController.navigationBar.translucent = false 
      navigationController.navigationBar.clipsToBounds = false 
      navigationController.navigationBar.shadowImage = nil 
      self.setNeedsStatusBarAppearanceUpdate() 
     } 

     func changeNavHairLines(navigationController:UINavigationController, show:Bool) { 
      for subview in navigationController.navigationBar.subviews { 
       for view in subview.subviews { 
        if(view.frame.size.height < 4) { 
         if show == true { 
          view.alpha = 1.0 
         } else { 
          view.alpha = 0.0 
         } 
        } 
       } 
      } 
     } 

要调用它使用:

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 
    // choose if you want to show or hide hairLines 
    changeNavHairLines(self.navigationController!,show:false) 
    customizeNavigationController(self.navigationController!) 
} 
+0

嗯,我很高兴将边框细线保留在除主页外的其他页面上。如果在加载主页时已经触发设置背景的代码,我该如何摆脱其他页面上的背景图像/阴影图像?对不起,如果这没有意义。 – zenpain

+0

好吧,看看现在,我已经添加了一个函数来删除这个奇怪的行 –

0
  1. 为了除去细黑线...在viewDidLoad和的viewWillAppear相关视图您想要移除细黑线的位置:

    //删除导航栏底部的细黑线。

    for parent in self.navigationController!.navigationBar.subviews { 
         for childView in parent.subviews { 
          if(childView is UIImageView) { 
           childView.removeFromSuperview() 
          } 
         } 
        } 
    
  2. 对于不同的导航栏的外观和它们的颜色和相关性能等为什么不自定义每个视图的导航栏属性,你做你的“主页”?

+0

我试过了,但是“//取得1px发际线”的代码通过主页按钮导航到其他页面 - 导致导航栏仍然是白色/透明的。我不知道如何撤消这个代码? – zenpain

0

navigationController中的navigationBar在整个应用程序中共享,所以在一个控制器中删除它将删除整个App。如何隐藏导航栏在你想要删除发际线并通过Interface Builder手动放置导航条的控制器中?

可以隐藏的导航栏,返回到主视图时:在viewWillAppear

self.navigationController?.setNavigationBarHidden(true, animated: true) 

广场这一点。

然后再次显示:

self.navigationController?.setNavigationBarHidden(false, animated: true) 

viewWillDisappear

+0

是的,这就是我想要做的。现在发生的事情是,当通过菜单栏访问时,我的模块页面navigationBar显示正常。但是,当通过主页上的按钮访问时,导航仍然隐藏...我想我需要通过viewWillDisappear撤消隐藏位,但我不知道如何。 – zenpain