gpt4 book ai didi

swift - 导航栏下方的菜单在 Swift 中带有动画

转载 作者:可可西里 更新时间:2023-11-01 01:20:15 24 4
gpt4 key购买 nike

我的问题很简单,当我点击(在导航栏上)时,我不知道如何在导航栏下方显示列表(菜单)。

我想做与这张图片相同的事情:

enter image description here

我试着这样做:

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
}
})

}
})


}
}

问题是,显示的 View 从导航栏上方经过,这不是我想要的效果。你知道我该怎么做吗?

最佳答案

这一行

self.window!.addSubview(infoView)

将其置于窗口中所有其他 View (包括导航栏)之上。

您应该搜索 View Controller 层次结构,从 rootViewController 开始,找到要将 View 添加到的 Controller 。

查看此问题的答案以获得一些选项:iPhone -- How to find topmost view controller

关于swift - 导航栏下方的菜单在 Swift 中带有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44270566/

24 4 0