为 select 视图控制器创建导航栏标题图像会产生奇怪的行为

Creating nav bar title image for select view controllers produces weird behavior

我使用以下函数在 select 视图控制器的导航栏标题位置创建了一个徽标:

class func setUpLogoOnNavBar(sourceVC: UIViewController) {

      dispatch_async(dispatch_get_main_queue(), { () -> Void in

         let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 135, height: 90))
         imageView.contentMode = .ScaleAspectFit
         let image = UIImage(named: "LogoAppHeaderBar.png")
         imageView.image = image

         sourceVC.navigationItem.titleView = imageView
      })
   }

通过使用 GCD 将它放在主线程上,当我执行标准的推送转场时,它会导致图像跳到导航栏的左侧,这看起来非常糟糕。当我从下面的函数中删除 GCD 代码时,徽标跳跃消失了。但是,我得到 This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. 此外,徽标不会在某些视图控制器中加载,因为(我假设)它没有在主线程上执行此操作。我该如何处理这个图像才能在主线程上加载,但在我继续时不会跳转?提前致谢!

为什么在布置 UI 时完全使用 GCD?它看起来不像是通过网络获取图像,因此不应异步布置导航栏。您是从另一个线程调用 setUpLogoOnNavBar 吗?你不应该。