【问题标题】:adding custom navigation bar in viewwillappear makes navigation slow?在 viewwillappear 中添加自定义导航栏会使导航变慢?
【发布时间】:2016-11-24 14:25:13
【问题描述】:

我创建了一个自定义导航栏。现在我将其添加到viewwillappear 方法中。因此,当我返回视图控制器时,加载需要时间。

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

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

【问题讨论】:

  • 检查图片尺寸是否太大

标签: ios swift xcode uinavigationcontroller uinavigationbar


【解决方案1】:

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

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

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

  2. storyboard本身中添加自定义导航栏,并在控制器中为其创建出口连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多