我有两个ViewControllers。我想在导航栏上添加一个UIView。Push Segue后添加UIView在导航栏上
我可以通过创建一个UINavigationItem出口以及将所述的UIView编程创建到UINavigationItem出口titleview的在第一导航栏的顶部添加的UIView。
代码片段(HomeVC - 拳套的ViewController):
class HomeVC: UIViewController {
@IBOutlet weak var homeNavigationItem: UINavigationItem!
let navBarView = UIView()
let topNavBarLabel = UILabel()
let screenWidth = UIScreen.main.bounds.width
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navBarView.frame = CGRect(x: 0.0, y: 0.0, width: screenWidth, height: 50.0)
topNavBarLabel.text = "Hello World"
topNavBarLabel.textAlignment = NSTextAlignment.center
topNavBarLabel.textColor = UIColor.white
topNavBarLabel.frame = CGRect(x: 0.0, y: 10.0, width: screenWidth, height: 20.0)
navBarView.addSubview(topNavBarLabel)
homeNavigationItem.titleView = navBarView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
截图景观:
现在柜面SecondVC的我试图做推后,同样的事情塞格。
代码片段(DetailVC - 第二的ViewController):
class DetailsVC: UIViewController {
let navBarView = UIView()
let screenWidth = UIScreen.main.bounds.width
let navBarTopLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navBarView.frame = CGRect(x: 0.0, y: 50.0, width: screenWidth, height: 50.0)
navBarView.backgroundColor = UIColor.black
navBarTopLabel.frame = CGRect(x: 0.0, y: 10.0, width: screenWidth, height: 20.0)
navBarTopLabel.textColor = UIColor.white
navBarTopLabel.text = "Details Hello"
navBarTopLabel.textAlignment = NSTextAlignment.center
navBarView.addSubview(navBarTopLabel)
self.view.addSubview(navBarView)
self.view.bringSubview(toFront: navBarView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
截图详情查看:
故事板截图:
视图层次:
注:我特意指派的Y位置到视图大于0,以确保正在创建
的看法,但因为我不能创建一个UINavigationItem,我将以编程方式创建的视图添加到视图中,甚至试图将子视图放在前面。
注:是的,UINavigationBar的的尺寸已经增大,请参考这里:Change width/height UINavigationBar embedded in a Navigation Controller