gpt4 book ai didi

ios - 在 viewwillappear 中添加自定义导航栏会使导航变慢?

转载 作者:行者123 更新时间:2023-11-30 12:52:34 27 4
gpt4 key购买 nike

我已经创建了一个自定义导航栏。现在我将其添加到 viewwillappear 方法中。因此,当我返回到 View Controller 时,需要一些时间来加载。

  func addViewToNav()
{
myView = UIView(frame: CGRect(x: 0, y: 20, width: (self.navigationController?.navigationBar.frame.size.width)!-40, height: (self.navigationController?.navigationBar.frame.size.height)!))

imageView.frame = CGRect(x: 5, y: (myView?.frame.size.height)!/2-14, width: 28, height: 28)
let image = UIImage(named: "download")
imageView.contentMode=UIViewContentMode.scaleAspectFill
imageView.image=image
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true
imageView.backgroundColor=UIColor.yellow
label = UILabel(frame: CGRect(x: imageView.frame.size.width+15, y: 0, width: 150, height: (myView?.frame.size.height)!))
label?.text="Label"
label?.textColor=UIColor.white
myView?.addSubview(imageView)
myView?.addSubview(label!)
self.navigationController?.view.addSubview(myView!)
//add tap gesturte on imageview & label
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
myView?.isUserInteractionEnabled=true
tapGesture.numberOfTapsRequired=1
myView?.addGestureRecognizer(tapGesture)
}

请指导我如何使其更快,因为这非常慢。

最佳答案

这需要时间,因为每次 Controller 出现在屏幕上时 viewWillAppear 都会被调用,并且每次创建自定义 View 、将其添加为 subview 然后添加手势识别器的代码都会被调用已执行。

有两种方法可以减少处理时间:

  1. 如果您想以编程方式执行此操作,请在 viewDidLoad(而不是 viewWillAppear)中添加代码来创建和添加自定义导航栏

    <
  2. storyboard 本身中添加自定义导航栏,并在 Controller 中为其创建 socket 连接。

关于ios - 在 viewwillappear 中添加自定义导航栏会使导航变慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com