我编写了以下代码来设置导航栏和视图控制器的视图:
AppDelegate的导航栏:
var navigationController = UINavigationController()
navigationController.viewControllers = [mainViewController]
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(它是一个表视图控制器)中做同样的事情:
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无法显示?
如何让它们都显示相同的半透明颜色导航栏?
答案 0 :(得分:0)
使用此方法更改UINavigationBar Globaly的所有位置,从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]
}
注意:您需要根据您的要求和用户界面修改它