2017-07-18 44 views
0

我寫了下面的代碼來設置導航欄和視圖控制器的看法:UINavigation條顏色

  1. 導航條在AppDelegate中:

    var navigationController = UINavigationController() 
    
    navigationController.viewControllers = [mainViewController] 
    
  2. MainViewController答:

    self.view.backgroundColor = UIColor.blue 
    let fullScreenRect = UIScreen.main.bounds 
    let buttonSize:CGFloat = 60 
    let viewLogButton = UIButton(frame: CGRect(x: 0, y: fullScreenRect.size.height-buttonSize, width:fullScreenRect.size.width, height: buttonSize)) 
    viewLogButton.setTitle("View Log", for: UIControlState.normal) 
    viewLogButton.setTitleColor(UIColor.white, for: .normal) 
    viewLogButton.setTitleColor(UIColor.blue, for: .highlighted) 
    viewLogButton.backgroundColor = UIColor.blue 
    viewLogButton.addTarget(self, action: #selector(viewLogButtonPressed), for: .touchUpInside) 
    self.view.addSubview(viewLogButton) 
    self.webView.frame = CGRect(x: 0, y: UIConstants.NAVIGATION_BAR_HEIGHT+UIConstants.STATUS_BAR_HEIGHT, width: fullScreenRect.size.width, height: fullScreenRect.size.height-buttonSize-UIConstants.NAVIGATION_BAR_HEIGHT-UIConstants.STATUS_BAR_HEIGHT) 
    self.view.addSubview(self.webview) 
    

導致頂部導航欄顯示半透明藍色的視圖。 MainViewController A

我然後做同樣在另一個的ViewController B(其是表視圖控制器):

  • MainViewController B:

    let tableView = UITableView() 
    let screenSize: CGRect = UIScreen.main.bounds  
    self.view.backgroundColor = UIColor.blue 
    let screenWidth = screenSize.width 
    let screenHeight = screenSize.height 
    tableView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)  
    self.view.addSubview(tableView) 
    
  • MainViewController B

    我想知道爲什麼MainVi中有半透明的導航條ewController A但MainViewController B無法顯示?

    我怎樣才能讓他們都顯示相同的半透明顏色導航欄?

    +0

    子類NavigationController和添加自定義屬性。這將在應用程序中隨處可見。 – Ellen

    回答

    0

    使用此方法Globaly改變UINavigationBar的所有地方,從AppDelegate

    func setupNaviBarAppearnce() { 
    
        UINavigationBar.appearance().isOpaque = true 
        UINavigationBar.appearance().isTranslucent = true 
        UINavigationBar.appearance().barTintColor = Theme.AppMainPurpleColor 
        UINavigationBar.appearance().tintColor = UIColor.white 
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:Typography.navigationTitle!,NSForegroundColorAttributeName:UIColor.white] 
    } 
    

    注意調用它:你需要修改它按照您的要求和UI

    +0

    我試過了。 isTranslucent,barTintColor在iOS10中不可用。 – user6539552

    +0

    不是'@available(iOS 3.0,*) open var isTranslucent:Bool //默認在iOS 6和更低版本上是NO。總是YES如果barStyle設置爲UIBarStyleBlackTranslucent' –

    +0

    對於條形色'@available(iOS 7.0,*) open var barTintColor:UIColor? // default is nil'只需命令並點擊'barTintColor' –