我正在尝试在所有其他视图之上的新窗口中添加UIAlertController。工作正常,但每次出现窗口时都试图打破自动布局。
let topWindow = UIApplication.sharedApplication().keyWindow!
let alertWin = UIWindow(frame: UIScreen.mainScreen().bounds)
alertWin.rootViewController = UIViewController()
alertWin.tintColor = UIApplication.sharedApplication().keyWindow!.tintColor
alertWin.windowLevel = topWindow.windowLevel + 1
alertWin.makeKeyAndVisible()
let alert = UIAlertController(title: "TITLE", message: text, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { _ in
alertWin.rootViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
alertWin.hidden = true
alertWin.rootViewController = nil
alertWin.removeFromSuperview()
}
alert.addAction(OKAction)
alertWin.rootViewController?.presentViewController(alert, animated: true, completion: nil)
我已经尝试将这个片段和动作闭包内容放入主线程中,但没有运气
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
Stack:(
0 CoreFoundation 0x000000010ed36d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010e0aedeb objc_exception_throw + 48
2 CoreFoundation 0x000000010ed36cbd +[NSException raise:format:] + 205
3 Foundation 0x000000010de22b6b _AssertAutolayoutOnMainThreadOnly + 79
4 Foundation 0x000000010dc82357 -[NSISEngine withBehaviors:performModifications:] + 31
5 UIKit 0x0000000111292adb -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 58
6 UIKit 0x000000011129361e -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 254
7 UIKit 0x0000000111283c47 -[UIWindow(UIConstraintBasedLayout) updateConstraintsIfNeeded] + 108
8 UIKit 0x0000000111294333 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 272
9 UIKit 0x0000000110a48a96 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
10 UIKit 0x0000000110a256f6 -[UIWindow layoutSubviews] + 43
11 UIKit 0x0000000110a58980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
12 QuartzCore 0x0000000110585c00 -[CALayer layoutSublayers] + 146
13 QuartzCore 0x000000011057a08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 QuartzCore 0x0000000110579f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x000000011056e3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
16 QuartzCore 0x000000011059c086 _ZN2CA11Transaction6commitEv + 486
17 QuartzCore 0x000000011059c378 _ZN2CA11Transaction14release_threadEPv + 224
18 libsystem_pthread.dylib 0x00000001139c8387 _pthread_tsd_cleanup + 470
19 libsystem_pthread.dylib 0x00000001139c7f63 _pthread_exit + 117
20 libsystem_pthread.dylib 0x00000001139c6582 pthread_attr_getschedpolicy + 0
21 libsystem_pthread.dylib 0x00000001139c4341 start_wqthread + 13
)