【问题标题】:UINavigation Bar ColorUI导航栏颜色
【发布时间】:2017-07-18 11:07:50
【问题描述】:

我已经编写了以下代码来设置导航栏和视图控制器的视图:

  1. AppDelegate 的导航栏:

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

    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)
    

这导致顶部导航栏的视图显示为半透明的蓝色。

然后我在另一个 ViewController B(它是一个表视图控制器)中做同样的事情:

  1. 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 A中有半透明导航栏,而MainViewController B却显示不出来?

如何让它们都显示相同的半透明彩色导航栏?

【问题讨论】:

  • 子类 NavigationController 并添加自定义属性。这将反映在应用程序的任何地方。

标签: ios uitableview uinavigationcontroller uinavigationbar


【解决方案1】:

使用这个方法改变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]
}

注意:您需要根据您的要求和用户界面对其进行修改

【讨论】:

  • 我试过了。 isTranslucent、barTintColor 在 iOS10 中不可用。
  • 不,是@available(iOS 3.0, *) open var isTranslucent: Bool // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
  • 对于条形颜色@available(iOS 7.0, *) open var barTintColor: UIColor? // default is nil 只需命令并单击barTintColor
  • 有帮助吗? ,或者你需要更多的东西?
  • 实际上,我的问题是为什么 ViewControllerA 顶部有一个半透明条,而 ViewController B 上没有这种颜色。我不明白我的代码的哪一部分导致了这种情况。 iOS10 中无法使用外观().isTranslucent
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 2020-08-12
  • 2017-12-07
相关资源
最近更新 更多