我的問題很簡單,我不知道如何顯示導航欄下面的列表(菜單),當我點擊它(在導航欄上)。Swift - 導航欄下面的菜單與動畫
我試着這樣做:
func doSomething(){
let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0
print(navigationBarHeight)
let heightTotal = UIApplication.shared.statusBarFrame.height + navigationBarHeight
DispatchQueue.main.async(execute: {
appDelegate.infoView(message: "test", Yorigin: heightTotal, color: colorBlueFollow)
})
}
而這的appDelegate:
func infoView(message: String, Yorigin: CGFloat ,color: UIColor){
if infoViewIsShowing == false{
infoViewIsShowing = true
// let infoViewHeight = self.window!.bounds.height/14.2
let infoViewHeight = self.window!.bounds.height/4.2
let infoViewY = Yorigin - infoViewHeight
let infoView = UIView(frame: CGRect(x: 0, y: infoViewY, width: self.window!.bounds.width, height: infoViewHeight))
infoView.backgroundColor = color
self.window!.addSubview(infoView)
let infoLabelWidth = infoView.bounds.width
let infoLabelHeight = infoView.bounds.height + UIApplication.shared.statusBarFrame.height/2
let infoLabel = UILabel()
infoLabel.frame.size.width = infoLabelWidth
infoLabel.frame.size.height = infoLabelHeight
infoLabel.numberOfLines = 0
infoLabel.text = message
infoLabel.font = UIFont(name: "HelveticaNeue", size: 11)
infoLabel.textColor = UIColor.white
infoLabel.textAlignment = .center
infoView.addSubview(infoLabel)
// Animate errorView
UIView.animate(withDuration: 0.2, animations: {
// Move down
infoView.frame.origin.y = Yorigin
}, completion: { (finished: Bool) in
if finished{
UIView.animate(withDuration: 0.2, delay: 3, options: .curveLinear, animations: {
// move up
infoView.frame.origin.y = infoViewY
}, completion: { (finished: Bool) in
if finished {
infoView.removeFromSuperview()
infoLabel.removeFromSuperview()
self.infoViewIsShowing = false
}
})
}
})
}
}
的問題是顯示的視圖正在通過導航欄上方,這不是我想要的效果...
你知道我該怎麼做嗎?
非常感謝你提前
感謝您的回答,我將我的代碼從AppDelegate移至當前的視圖控制器,並且我更改了self.window! self.view。就是那麼簡單啊! – KevinB