2017-05-01 47 views
0

我已經寫在迅速的iOS應用覆蓋缺省導航欄背景AppDelegateUINavigationBar的外觀UIDocumentInteractionController

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    let navBackgroundImage = UIImage(named: "navBck") 
    UINavigationBar.appearance().barTintColor = UIColor(patternImage: navBackgroundImage) 
} 

在我想用一個UIDocumentInteractionController來顯示PDF文件的應用生成的應用程序。

let docInteractionController = UIDocumentInteractionController(url: pdfURL!) 
docInteractionController.delegate = self 
docInteractionController.presentPreview(animated: true) 

的問題是,自定義背景我似乎並不支持,這與以下錯誤崩潰

斷言失敗(這並不沒有AppDelegate自定義背景發生) - [ UICGColor encodeWithCoder:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.7.47/UIColor.m:1549 由於未捕獲的異常「NSInternalInconsistencyException」而終止應用程序,原因:'僅限RGBA或在這種情況下,支持白色色彩空間。'

我正在尋找最簡單最簡單的方法來解決此問題,方法是將ARGB背景恢復爲僅用於此操作的導航欄。我試圖在我的UIDocumentInteractionControllerDelegate執行下面沒有運氣(有相同的錯誤崩潰)

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { 
    UINavigationBar.appearance().barTintColor = UIColor.darkGrey 
    return self 
} 

func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) 
{ 
    let navBackgroundImage:UIImage! = UIImage(named: "navBck") 
    UINavigationBar.appearance().barTintColor = UIColor(patternImage: navBackgroundImage) 
} 
+0

嘗試在呈現UIDocumentInteractionController之前設置barTintColor。 – tmrog

+0

剛剛嘗試過,這給出了相同的錯誤 – tishu

回答

0

從UIDocumentInteractionControllerDelegate的文件的摘錄:

//如果預覽支持,這提供了將顯示預覽的視圖控制器。

//如果支持預覽,則此方法是必需的。

// 如果在導航堆棧頂部顯示,請提供導航控制器以便以與平臺其餘部分一致的方式進行動畫播放。

@available(iOS 3.2,*)

optional public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController

而且,在我的新項目,我一直用navigationController?.navigationBar.barTintColor代替UINavigationBar.appearance().barTintColor

編輯:新的例子,與UIDocumentInteractionController

視圖控制器的更多瞭解,提出了UIDocumentInteractionController

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     navigationController?.navigationBar.barTintColor = getCustomTintColor 
    } 

    func getCustomTintColor() -> UIColor{ 
     let navBackgroundImage:UIImage! = UIImage(named: "navBck") 
     return UIColor(patternImage: navBackgroundImage) 
    } 

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { 
     navigationController?.navigationBar.barTintColor = UIColor. darkGrey 
     return navigationController! 
    } 

    func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) { 
     navigationController?.navigationBar.barTintColor = getCustomTintColor 
    } 

說實話,我也覺得甚至崩潰不會發生在所有如果你

  1. 使用navigationController?.navigationBar.barTintColor代替UINavigationBar.appearance().barTintColor
  2. 返回navigationController在documentInteractionControllerViewControllerForPreview()

這樣,你的預覽畫面也會有正確的色彩。 請提供圖片,以便我可以檢查我的示例項目(或者在您的代碼中檢查自己:))。

+0

UIDocumentInteractionController沒有擴展UIViewController,所以我看不到navigationController.pushViewController(docInteractionController,animated:true)如何編譯。用docInteractionController.presentPreview替換它(動畫:true)失敗,按照我上面的註釋 – tishu

+0

我的錯誤,我將編輯代碼 – 7to4

+0

在我上一個示例項目中,我創建了一個隨機VC並進行了檢查。現在,我已通過實際打開pdf進行驗證。 – 7to4

相關問題