setTranslatesAutoresizingMaskIntoConstraints和setFrame组合使用导致的异常
Posted 3bugs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了setTranslatesAutoresizingMaskIntoConstraints和setFrame组合使用导致的异常相关的知识,希望对你有一定的参考价值。
在用Ojbect-c开发OSX应用的时候需要用到自定义控件并用代码进行布局,很自然地就使用了setTranslatesAutoresizingMaskIntoConstraints和setFrame组合,同时这个组合也很好的满足的需求。但是每当我先当前项目添加一个异常断点的时候,并开启调试程序,总是停在程序刚启动的main函数。经过debug发现是应为setTranslatesAutoresizingMaskIntoConstraints和setFrame组合导致了这个异常。
代码
[_programHeadView setTranslatesAutoresizingMaskIntoConstraints:YES];
[_programHeadView setFrame:headRect];
解决办法
在调用setTranslatesAutoresizingMaskIntoConstraints和setFrame之前先把view从父view用移除,调用之后再添加回去这样就能避免当前view的constraints和默认的constraints产生冲突。
[_programHeadView removeFromSuperview];
[_programHeadView setTranslatesAutoresizingMaskIntoConstraints:YES];
[_programHeadView setFrame:headRect];
[[[self window] contentView] addSubview:_programHeadView];
异常信息
* thread #1: tid = 0x6931b, 0x00007fffb1b5745d libobjc.A.dylib`objc_exception_throw, queue = ‘com.apple.main-thread‘, stop reason = breakpoint 1.1 * frame #0: 0x00007fffb1b5745d libobjc.A.dylib`objc_exception_throw frame #1: 0x00007fff9cde1c3d CoreFoundation`+[NSException raise:format:] + 205 frame #2: 0x00007fff9b2a7014 AppKit`LAYOUT_CONSTRAINTS_NOT_SATISFIABLE + 45 frame #3: 0x00007fff9b2a717e AppKit`-[NSView(NSConstraintBasedLayout) engine:willBreakConstraint:dueToMutuallyExclusiveConstraints:] + 267 frame #4: 0x00007fff9e994fea Foundation`-[NSISEngine handleUnsatisfiableRowWithHead:body:usingInfeasibilityHandlingBehavior:mutuallyExclusiveConstraints:] + 507 frame #5: 0x00007fff9e996006 Foundation`-[NSISEngine tryUsingArtificialVariableToAddConstraintWithMarker:rowBody:usingInfeasibilityHandlingBehavior:mutuallyExclusiveConstraints:] + 482 frame #6: 0x00007fff9e729325 Foundation`-[NSISEngine tryToAddConstraintWithMarker:expression:integralizationAdjustment:mutuallyExclusiveConstraints:] + 730 frame #7: 0x00007fff9e99ea01 Foundation`-[NSLayoutConstraint _addLoweredExpression:toEngine:integralizationAdjustment:lastLoweredConstantWasRounded:mutuallyExclusiveConstraints:] + 281 frame #8: 0x00007fff9e7207ac Foundation`-[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 236 frame #9: 0x00007fff9a822033 AppKit`-[NSView(NSConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:integralizationAdjustment:mutuallyExclusiveConstraints:] + 91 frame #10: 0x00007fff9a7b68ba AppKit`-[NSView(NSConstraintBasedLayout) _constraints_didChangeAutoresizingConstraintsArrayForContainedView:] + 224 frame #11: 0x00007fff9a83c3ed AppKit`-[NSView updateConstraints] + 139 frame #12: 0x00007fff9a83bf27 AppKit`-[NSView _updateConstraintsForSubtreeIfNeededCollectingViewsWithInvalidBaselines:] + 989 frame #13: 0x00007fff9a83bd31 AppKit`-[NSView _updateConstraintsForSubtreeIfNeededCollectingViewsWithInvalidBaselines:] + 487 frame #14: 0x00007fff9a884c26 AppKit`__82-[NSView _updateConstraintsForSubtreeIfNeededCollectingViewsWithInvalidBaselines:]_block_invoke + 290 frame #15: 0x00007fff9e726adb Foundation`-[NSISEngine withBehaviors:performModifications:] + 155 frame #16: 0x00007fff9a83bc33 AppKit`-[NSView _updateConstraintsForSubtreeIfNeededCollectingViewsWithInvalidBaselines:] + 233 frame #17: 0x00007fff9b059c84 AppKit`__46-[NSView _updateConstraintsForSubtreeIfNeeded]_block_invoke + 49 frame #18: 0x00007fff9e726adb Foundation`-[NSISEngine withBehaviors:performModifications:] + 155 frame #19: 0x00007fff9a7b5760 AppKit`-[NSView(NSConstraintBasedLayout) _withAutomaticEngineOptimizationDisabled:] + 70 frame #20: 0x00007fff9b059a79 AppKit`-[NSView _updateConstraintsForSubtreeIfNeeded] + 224 frame #21: 0x00007fff9b2acbb8 AppKit`-[NSWindow(NSConstraintBasedLayout) _updateConstraintsForEngineHostingViews:] + 76 frame #22: 0x00007fff9a884a7a AppKit`__62-[NSWindow(NSConstraintBasedLayout) updateConstraintsIfNeeded]_block_invoke + 159 frame #23: 0x00007fff9e726adb Foundation`-[NSISEngine withBehaviors:performModifications:] + 155 frame #24: 0x00007fff9a7b5760 AppKit`-[NSView(NSConstraintBasedLayout) _withAutomaticEngineOptimizationDisabled:] + 70 frame #25: 0x00007fff9a88496e AppKit`-[NSWindow(NSConstraintBasedLayout) updateConstraintsIfNeeded] + 245 frame #26: 0x00007fff9a9248fb AppKit`___NSWindowGetDisplayCycleObserver_block_invoke + 73 frame #27: 0x00007fff9a924831 AppKit`__37+[NSDisplayCycle currentDisplayCycle]_block_invoke + 622 frame #28: 0x00007fffa2802dc6 QuartzCore`CA::Transaction::run_commit_handlers(CATransactionPhase) + 46 frame #29: 0x00007fffa290c8f0 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 160 frame #30: 0x00007fffa28018a1 QuartzCore`CA::Transaction::commit() + 475 frame #31: 0x00007fff9ac07b99 AppKit`__37+[NSDisplayCycle currentDisplayCycle]_block_invoke.31 + 323 frame #32: 0x00007fff9ccf8dd7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 frame #33: 0x00007fff9ccf8d47 CoreFoundation`__CFRunLoopDoObservers + 391 frame #34: 0x00007fff9ccd9789 CoreFoundation`__CFRunLoopRun + 873 frame #35: 0x00007fff9ccd91c4 CoreFoundation`CFRunLoopRunSpecific + 420 frame #36: 0x00007fff9c23aebc HIToolbox`RunCurrentEventLoopInMode + 240 frame #37: 0x00007fff9c23abf9 HIToolbox`ReceiveNextEventCommon + 184 frame #38: 0x00007fff9c23ab26 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 71 frame #39: 0x00007fff9a7d5e24 AppKit`_DPSNextEvent + 1120 frame #40: 0x00007fff9af5185e AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796 frame #41: 0x00007fff9a7ca7ab AppKit`-[NSApplication run] + 926 frame #42: 0x00007fff9a7951de AppKit`NSApplicationMain + 1237
以上是关于setTranslatesAutoresizingMaskIntoConstraints和setFrame组合使用导致的异常的主要内容,如果未能解决你的问题,请参考以下文章