簡體   English   中英

應用程序在調用自定義按鈕的 setNeedsDisplay 時崩潰

[英]App crashes on call to setNeedsDisplay for custom button

我有自定義按鈕,應用程序在調用setNeedsDisplay隨機崩潰。 如何修復這個崩潰?

class MyVC: UIViewController {

    @IBOutlet weak var customButton: CustomButton!

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)

        self.customButton.setNeedsDisplay() // Random crash here
    }
}

class CustomButton: UIButton {
    // ....

    override func setNeedsDisplay() {
        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: UIScreen.main.bounds.size.width-29, bottom: 4.0, right: 0)
    }
}

崩潰日志

崩潰:com.apple.main-thread EXC_BREAKPOINT 0x0000000104fb3f48

崩潰:com.apple.main-thread 0 myapp 0x104fb3f48 MyVC.viewWillTransition(to:with:) + 63 (MyVC.swift:63) 1 myapp
0x104fb3f88 @objc MyVC.viewWillTransition(到:與:)()2 UIKitCore 0x1ee286708 - [UIViewController中viewWillTransitionToSize:withTransitionCoordinator:] + 868 3 UIKitCore 0x1ee1e596c - [UINavigationController的viewWillTransitionToSize:withTransitionCoordinator:] + 84 4 UIKitCore 0x1ee286708 - [UIViewController中viewWillTransitionToSize:withTransitionCoordinator: ] + 868 5 UIKitCore 0x1ee1b46dc - [的UITabBarController viewWillTransitionToSize:withTransitionCoordinator:] + 48 6 UIKitCore 0x1ee27ba18 + [UIViewController中_performWithoutDeferringTransitions:] + 112 7 UIKitCore 0x1ee292a24 - [UIViewController中(AdaptiveSizing)_window:viewWillTransitionToSize:withTransitionCoordinator:] + 580 8 UIKitCore 0x1ee8537c4 59- [UIWindow _rotateToBounds:withAnimator:transitionContext:]_block_invoke + 188 9 UIKitCore 0x1eeca08f0 +[UIView(Animation) performWithoutAnimation:] + 104 10 UIKitCore
0x1ee853618 -[UIWindow _rotateToBounds:withAnimator:transitionContext:] + 412 11 UIKitCore 0x1ee855e24 -[UIWindow _rotateWindowToOrientation:updateStatusBar:duration:skipCallbacks:] + 1184 1184 1180000:12 UIKit85Roys:updateeBarsetRoys:updateeetRollingBar:updateeetRolling: 0x1ee855e24 516 13 UIKitCore 0x1ee8558d8 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 128 14 UIKitCore 0x1ee854584 __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block18Interface:force oke18Interface_18Interface:force oke18UIKit_18Interface:force _block_in48 16 UIKitCore 0x1ee0dcfe0 -[_UICanvasMetricsCalculator updateMetricsOnWindows:animated:] + 624 17 UIKitCore
0x1ee0e1990 -[_UICanvas _computeMetrics:] + 180 18 UIKitCore
0x1eeca08f0 + [UIView的(動畫)performWithoutAnimation:] + 104 19 UIKitCore 0x1ee0e04a8 - [_ UICanvas _performActions:withOverrideSettings:] + 172 20 UIKitCore 0x1ee821b0c - [UIApplication的_createSnapshotContextForScene:withName:performLayoutWithSettings:] + 404 21 UIKitCore 0x1ee8232f0 __65- [UIApplication的_performSnapshotsWithAction:forScene :完成:] _ block_invoke_2 + 124個22 FrontBoardServices 0x1c4b51768 - [FBSSceneSnapshotAction _executeNextRequest] + 256個23 FrontBoardServices 0x1c4b517b8 - [FBSSceneSnapshotAction _executeNextRequest] + 336個24 FrontBoardServices 0x1c4b51358 - [FBSSceneSnapshotAction executeRequestsWithHandler:completionHandler:expirationHandler:] + 276 25 UIKitCore 0x1ee823220 __65- [UIApplication的_performSnapshotsWithAction :forScene:completion:]_block_invoke + 268 26 UIKitCore 0x1ee822848 -[UIApplication _beginSnapshotSessionForScene:withSnapshotBlock:] + 744 27 UIKitCore 0x1ee822f74 -[UIApplication _performSnapshotsWith8Action:for8Core:forScene 0x1ee822e40 - [UIApplication的_handleSnapshotAction:forScene:完成:] + 156 29 UIKitCore 0x1ee81e724 __71- [UIApplication的_handleSnapshotAction:forScene:deactivationCompletion:] _ block_invoke + 332 30 UIKitCore 0x1ee81e540 - [UIApplication的_handleSnapshotAction:forScene:deactivationCompletion:] + 340 31 UIKitCore 0x1ee0d9f58 __98- [__UICanvasLifecycleMonitor_Compatability deactivateEventsOnly:withContext:forceExit:完成:] _ block_invoke.261 + 820個32個UIKitCore 0x1ee8220ac _runAfterCACommitDeferredBlocks + 296 33 UIKitCore 0x1ee810bfc _cleanUpAfterCAFlushAndRunDeferredBlocks + 352 34 UIKitCore 0x1ee83da6c _afterCACommitHandler + 116 35的CoreFoundation 0x1c212ed08 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 32 36的CoreFoundation 0x1c2129a30 __CFRunLoopDoObservers + 412 37的CoreFoundation 0x1c2129fac __CFRunLoopRun + 1228 38 CoreFoundation 0x1c21297c0 CFRunLoopRunSpecific + 436 39 圖形服務
0x1c432a79c GSEventRunModal + 104 40 UIKitCore
0x1ee816c38 UIApplicationMain + 212 41 myapp
0x104db7454 main + 20 (ProfileVC.swift:20) 42 libdyld.dylib
0x1c1bed8e0 開始 + 4

假設您的崩潰行實際上應該是:

self.customButton.setNeedsDisplay() // Random crash here

問題很可能是customButton為零。 您已將customButton聲明為隱式展開的可選項。 這意味着當您嘗試引用它時,編譯器會為您解開它。 嘗試添加:

print("self.customButton = \(self.customButton?)")
self.customButton.setNeedsDisplay() // Random crash here

並查看調試日志。 我猜你會看到類似的東西:

self.customButton = Optional(nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM