Adding constraints programmatically to IB Scene - ios

I'm trying to add a subview and some constraints to a scene, which is build using the interface builder.
The subview gets added correctly, but when I try to add the constraints the app crashes with the following error, which implies that I've got the view hierarchy mixed up. However I'm can't find my error.
Shopigator[14377:635114] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fafab4bb660 THSegmentedControl:0x7fafaf03a6e0.centerX == UISearchBar:0x7fafaf0334b0.centerX>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-03-13 11:51:12.004 Shopigator[14377:635114] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fafab4bb660 THSegmentedControl:0x7fafaf03a6e0.centerX == UISearchBar:0x7fafaf0334b0.centerX>
Container hierarchy:
<UIView: 0x7fafaf037d70; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fafab424a00>>
| <_UILayoutGuide: 0x7fafaf037e40; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fafa97e4710>>
| <_UILayoutGuide: 0x7fafaf037540; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fafaf0ed980>>
| <UIView: 0x7fafaf035e40; frame = (0 0; 500 50); layer = <CALayer: 0x7fafaf0a9be0>>
| <THSegmentedControl: 0x7fafaf03a6e0; baseClass = UIControl; frame = (34 297; 332 29); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7fafaf0398d0>>
View not found in container hierarchy: <UISearchBar: 0x7fafaf0334b0; frame = (0 0; 320 44); text = ''; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fafb10ad1f0>; layer = <CALayer: 0x7fafab44ed10>>
That view's superview: NO SUPERVIEW
2015-03-13 11:51:12.060 Shopigator[14377:635114] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7fafab4bb660 THSegmentedControl:0x7fafaf03a6e0.centerX == UISearchBar:0x7fafaf0334b0.centerX> view:<UIView: 0x7fafaf037d70; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fafab424a00>>'
*** First throw call stack:
(
0 CoreFoundation 0x0000000108a2df35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001086c6bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000108a2de6d +[NSException raise:format:] + 205
3 Foundation 0x0000000107f6e0f9 -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187
4 UIKit 0x0000000109b77de5 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
5 Foundation 0x0000000107f7bd6e -[NSISEngine withBehaviors:performModifications:] + 155
6 UIKit 0x0000000109b77beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
7 UIKit 0x0000000109b779fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
8 UIKit 0x0000000109b77671 -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
9 UIKit 0x0000000109b78191 -[UIView(AdditionalLayoutSupport) _layoutEngineCreateIfNecessary] + 53
10 UIKit 0x0000000109b6cb0a -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 156
11 UIKit 0x0000000109b6cea4 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
12 UIKit 0x0000000109b6cfcc -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
13 Shopigator 0x0000000107507617 _TFC10Shopigator20FilterViewController11viewDidLoadfS0_FT_T_ + 9623
14 Shopigator 0x0000000107508d92 _TToFC10Shopigator20FilterViewController11viewDidLoadfS0_FT_T_ + 34
15 UIKit 0x0000000109617a90 -[UIViewController loadViewIfRequired] + 738
16 UIKit 0x0000000109617c8e -[UIViewController view] + 27
17 UIKit 0x0000000109bb941e -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
18 UIKit 0x00000001095f3429 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
19 UIKit 0x0000000109623a41 -[UIViewController _presentViewController:withAnimationController:completion:] + 1746
20 UIKit 0x0000000109625d81 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
21 UIKit 0x0000000109625ca5 -[UIViewController presentViewController:animated:completion:] + 229
22 UIKit 0x00000001094f38be -[UIApplication sendAction:to:from:forEvent:] + 75
23 UIKit 0x00000001095fa410 -[UIControl _sendActionsForEvents:withEvent:] + 467
24 UIKit 0x00000001095f97df -[UIControl touchesEnded:withEvent:] + 522
25 UIKit 0x0000000109539308 -[UIWindow _sendTouchesForEvent:] + 735
26 UIKit 0x0000000109539c33 -[UIWindow sendEvent:] + 683
27 UIKit 0x00000001095069b1 -[UIApplication sendEvent:] + 246
28 UIKit 0x0000000109513a7d _UIApplicationHandleEventFromQueueEvent + 17370
29 UIKit 0x00000001094ef103 _UIApplicationHandleEventQueue + 1961
30 CoreFoundation 0x0000000108963551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
31 CoreFoundation 0x000000010895941d __CFRunLoopDoSources0 + 269
32 CoreFoundation 0x0000000108958a54 __CFRunLoopRun + 868
33 CoreFoundation 0x0000000108958486 CFRunLoopRunSpecific + 470
34 GraphicsServices 0x000000010b2849f0 GSEventRunModal + 161
35 UIKit 0x00000001094f2420 UIApplicationMain + 1282
36 Shopigator 0x000000010748081e top_level_code + 78
37 Shopigator 0x000000010748085a main + 42
38 libdyld.dylib 0x000000010b7a8145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Relevant parts of my ViewController
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var radiusSlider: LoagarithmicSlider!
var priceClassSelector: THSegmentedControl?
override func viewDidLoad() {
super.viewDidLoad()
var pCFrame = CGRectMake(34, 297, 332, 29)
self.priceClassSelector = THSegmentedControl(segments: SearchFilter.PRICE_CLASS_LABELS)
self.priceClassSelector!.frame = pCFrame
self.priceClassSelector?.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.priceClassSelector!)
self.view.addConstraint(NSLayoutConstraint(
item: self.priceClassSelector!,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self.searchBar,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1.0,
constant: 0))
}
And the view hierarchy in IB

Related

Unrecognized Selector Instance When Performing Segue

When performing programatically performing a segue, the segue does work and the intended storyboard opens, but terminates immediately afterwards. The view controller/storyboard does work and shouldn't have any errors in the code as I've tested it individually, so I'm not entirely sure as to why it terminates. Any help would be appreciated.
2017-05-01 20:22:59.358605 FInal Project[15659:499662] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/student/Library/Developer/CoreSimulator/Devices/265EA47F-07A6-47C7-A6B4-5E62D37E72BA/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-05-01 20:22:59.376075 FInal Project[15659:499662] [MC] Reading from private effective user settings.
in
2017-05-01 20:23:48.095 FInal Project[15659:499662] <UILayoutContainerView: 0x7fdae3c082f0; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x600000242be0>; layer = <CALayer: 0x60000003ea40>>'s window is not equal to <UINavigationController: 0x7fdae40aee00>'s view's window!
2017-05-01 20:23:48.715 FInal Project[15659:499662] -[FInal_Project.User_Info NameInputEditingDidEnd:]: unrecognized selector sent to instance 0x7fdae3c11d40
2017-05-01 20:23:48.721 FInal Project[15659:499662] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FInal_Project.User_Info NameInputEditingDidEnd:]: unrecognized selector sent to instance 0x7fdae3c11d40'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101fc3d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000101a2521e objc_exception_throw + 48
2 CoreFoundation 0x0000000102033f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000101f49005 ___forwarding___ + 1013
4 CoreFoundation 0x0000000101f48b88 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001030cb8bc -[UIApplication sendAction:to:from:forEvent:] + 83
6 UIKit 0x0000000103251c38 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000103251f51 -[UIControl _sendActionsForEvents:withEvent:] + 444
8 UIKit 0x0000000103bdbc20 -[UITextField _resignFirstResponder] + 313
9 UIKit 0x00000001032e1778 -[UIResponder _finishResignFirstResponder] + 286
10 UIKit 0x0000000103bdba0e -[UITextField _finishResignFirstResponder] + 49
11 UIKit 0x00000001032e1827 -[UIResponder resignFirstResponder] + 140
12 UIKit 0x0000000103bdb8dd -[UITextField resignFirstResponder] + 136
13 UIKit 0x000000010317802f -[UIView(Hierarchy) _removeFirstResponderFromSubtree] + 167
14 UIKit 0x00000001031786b8 __UIViewWillBeRemovedFromSuperview + 76
15 UIKit 0x000000010317846a -[UIView(Hierarchy) removeFromSuperview] + 95
16 UIKit 0x000000010324d451 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke.629 + 704
17 UIKit 0x0000000103246fba -[UIPresentationController transitionDidFinish:] + 111
18 UIKit 0x000000010345ef83 -[_UICurrentContextPresentationController transitionDidFinish:] + 42
19 UIKit 0x000000010324aef0 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 183
20 UIKit 0x0000000103c0c56c -[_UIViewControllerTransitionContext completeTransition:] + 102
21 UIKit 0x0000000103243ddc -[UITransitionView notifyDidCompleteTransition:] + 251
22 UIKit 0x0000000103243aef -[UITransitionView _didCompleteTransition:] + 1539
23 UIKit 0x000000010324651c -[UITransitionView _transitionDidStop:finished:] + 104
24 UIKit 0x0000000103156bd5 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 222
25 UIKit 0x000000010315712a -[UIViewAnimationState animationDidStop:finished:] + 136
26 QuartzCore 0x0000000108526648 _ZN2CA5Layer23run_animation_callbacksEPv + 316
27 libdispatch.dylib 0x0000000105e2e0cd _dispatch_client_callout + 8
28 libdispatch.dylib 0x0000000105e0e8a4 _dispatch_main_queue_callback_4CF + 406
29 CoreFoundation 0x0000000101f87e49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
30 CoreFoundation 0x0000000101f4d37d __CFRunLoopRun + 2205
31 CoreFoundation 0x0000000101f4c884 CFRunLoopRunSpecific + 420
32 GraphicsServices 0x0000000107d38a6f GSEventRunModal + 161
33 UIKit 0x00000001030c9c68 UIApplicationMain + 159
34 FInal Project 0x00000001013fb65f main + 111
35 libdyld.dylib 0x0000000105e7a68d start + 1
36 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The issue (discovered in the comments above) was that there was an action in the storyboard that was no longer hooked up to an action in code.
If anyone else in the future has a similar problem, look through the outlets menu for an outlet or action with the name provided in the console output. In this case, the action was named NameInputEditingDidEnd.
To expand on Nathan's answer, instead of randomly looking around, just cmd+shift+f the name of the outlet, in this case NameInputEditingDidEnd, and xcode will output the storyboard with the dangling outlet for you and center you to it.

Core Data cannot save data of UITextField in IOS Objective - C

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Unacceptable type of value for attribute: property = "name";
desired type = NSString; given type = UITextField; value =
<UITextField: 0x7fb13a63f7d0; frame = (39 246; 297 30); text =
myworld'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM;
gestureRecognizers = <NSArray: 0x7fb13a71f740>; layer = <CALayer: 0x7fb13a63fa80>>.'
First throw call stack:
(
0 CoreFoundation 0x000000010da88c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d71fbb7 objc_exception_throw + 45
2 CoreData 0x000000010cf208a3 _PFManagedObject_coerceValueForKeyWithDescription + 3203
3 CoreData 0x000000010cef92f1 _sharedIMPL_setvfk_core + 177
4 MyStore 0x000000010ce2b2f7 -[DetailViewController save:] + 183
5 UIKit 0x000000010de83d62 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010de83d62 -[UIApplication sendAction:to:from:forEvent:] + 75
7 UIKit 0x000000010df9550a -[UIControl _sendActionsForEvents:withEvent:] + 467
8 UIKit 0x000000010df948d9 -[UIControl touchesEnded:withEvent:] + 522
9 UIKit 0x000000010ded0958 -[UIWindow _sendTouchesForEvent:] + 735
10 UIKit 0x000000010ded1282 -[UIWindow sendEvent:] + 682
11 UIKit 0x000000010de97541 -[UIApplication sendEvent:] + 246
12 UIKit 0x000000010dea4cdc _UIApplicationHandleEventFromQueueEvent + 18265
13 UIKit 0x000000010de7f59c _UIApplicationHandleEventQueue + 2066
14 CoreFoundation 0x000000010d9bc431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x000000010d9b22fd __CFRunLoopDoSources0 + 269
16 CoreFoundation 0x000000010d9b1934 __CFRunLoopRun + 868
17 CoreFoundation 0x000000010d9b1366 CFRunLoopRunSpecific + 470
18 GraphicsServices 0x0000000110feaa3e GSEventRunModal + 161
19 UIKit 0x000000010de828c0 UIApplicationMain + 1282
20 MyStore 0x000000010ce2b07f main + 111
21 libdyld.dylib 0x0000000110156145 start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
As per crash report error is telling you, the value you are giving is a UITextField instead of textfield.text i.e of NSString for "name" property
For Similar issue you can refer here
unacceptable-type-of-value-for-attribute-property

App termination after applying constraints programmatically

I have two views, establish the subviews, create a constraint, then apply it. myView and topview are two UIViews under 'myOverylingView'
import UIKit
class ViewController: UIViewController {
#IBOutlet var myOverlyingView: UIView!
#IBOutlet weak var myView: UIView!
var x : Int = 500;
override func viewDidLoad() {
super.viewDidLoad()
var topview = NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(self.myView)) as? UIView
myOverlyingView.addSubview(topview!)
topview!.backgroundColor = UIColor.blueColor()
var myConstraint : NSLayoutConstraint = NSLayoutConstraint(item: topview!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.myView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
topview!.addConstraint(myConstraint)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
After applying the constraint I get a termination error as follows.
2015-06-03 14:03:00.716 threadingTest1[30147:4645926] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb43bb00500 UIView:0x7fb43bb00800.centerX == UIView:0x7fb43941ff30.centerX>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-06-03 14:03:00.720 threadingTest1[30147:4645926] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fb43bb00500 UIView:0x7fb43bb00800.centerX == UIView:0x7fb43941ff30.centerX>
Container hierarchy:
<UIView: 0x7fb43bb00800; frame = (150 100; 300 65); autoresize = RM+BM; layer = <CALayer: 0x7fb43bb00000>>
View not found in container hierarchy: <UIView: 0x7fb43941ff30; frame = (150 100; 300 65); autoresize = RM+BM; layer = <CALayer: 0x7fb43941ef20>>
That view's superview: <UIView: 0x7fb43941fc20; frame = (0 0; 375 667); autoresize = RM+BM; layer = <CALayer: 0x7fb43941f260>>
2015-06-03 14:03:00.725 threadingTest1[30147:4645926] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7fb43bb00500 UIView:0x7fb43bb00800.centerX == UIView:0x7fb43941ff30.centerX> view:<UIView: 0x7fb43bb00800; frame = (150 100; 300 65); autoresize = RM+BM; layer = <CALayer: 0x7fb43bb00000>>'
*** First throw call stack:
(
0 CoreFoundation 0x0000000100c69c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001027d4bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000100c69b9d +[NSException raise:format:] + 205
3 Foundation 0x0000000101083479 -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187
4 UIKit 0x0000000101b83a34 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
5 Foundation 0x00000001010911be -[NSISEngine withBehaviors:performModifications:] + 155
6 UIKit 0x0000000101b8383a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
7 UIKit 0x0000000101b8364d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
8 UIKit 0x0000000101b83933 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
9 Foundation 0x00000001010911be -[NSISEngine withBehaviors:performModifications:] + 155
10 UIKit 0x0000000101b8383a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
11 UIKit 0x0000000101b8364d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
12 UIKit 0x0000000101b832de -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
13 UIKit 0x0000000101b83de1 -[UIView(AdditionalLayoutSupport) _layoutEngineCreateIfNecessary] + 53
14 UIKit 0x0000000101b787e7 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 156
15 UIKit 0x0000000101b78b74 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
16 UIKit 0x0000000101b78c9c -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
17 threadingTest1 0x0000000100a6e625 _TFC14threadingTest114ViewController11viewDidLoadfS0_FT_T_ + 1637
18 threadingTest1 0x0000000100a6e6d2 _TToFC14threadingTest114ViewController11viewDidLoadfS0_FT_T_ + 34
19 UIKit 0x0000000101639210 -[UIViewController loadViewIfRequired] + 738
20 UIKit 0x000000010163940e -[UIViewController view] + 27
21 UIKit 0x00000001015542c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
22 UIKit 0x000000010155468f -[UIWindow _setHidden:forced:] + 247
23 UIKit 0x0000000101560e21 -[UIWindow makeKeyAndVisible] + 42
24 UIKit 0x0000000101504457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
25 UIKit 0x00000001015071de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
26 UIKit 0x00000001015060d5 -[UIApplication workspaceDidEndTransaction:] + 179
27 FrontBoardServices 0x00000001042e45e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
28 CoreFoundation 0x0000000100b9d41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
29 CoreFoundation 0x0000000100b93165 __CFRunLoopDoBlocks + 341
30 CoreFoundation 0x0000000100b92f25 __CFRunLoopRun + 2389
31 CoreFoundation 0x0000000100b92366 CFRunLoopRunSpecific + 470
32 UIKit 0x0000000101505b42 -[UIApplication _run] + 413
33 UIKit 0x0000000101508900 UIApplicationMain + 1282
34 threadingTest1 0x0000000100a75057 main + 135
35 libdyld.dylib 0x0000000102f2c145 start + 1
36 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
This first portion of the error message stands out to me as it asks if I am trying to constrain something outside the subtree of the view.
I am trying to follow this understanding of the view hierarchy here, Mac Developer Library, though I may be interpreting it incorrectly.
Any help would be appreciated.
This makes no sense. You say this:
#IBOutlet weak var myView: UIView!
But you also say this:
myOverlyingView.addSubview(self.myView)
If you are expecting myView to arrive as an outlet, then it must be in the interface already. So how can you then add it as a subview to anything?
If it is not arriving as an outlet, then it is nil, and that explains why you can't set up a constraint to it.
Either way, those two lines are incoherent in relation to one another.

Auto Layout Crashes When Adding Constraints to UITableView

I am trying to add a UIView subview to a UITableView that will rotate appropriately with the device. I want the subview to cover the entire table. Here is my method, called from viewDidLoad:
-(void)welcomeScreen {
UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
newView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
newView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:newView];
NSDictionary *views = NSDictionaryOfVariableBindings(newView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[newView]|"
options:0
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[newView]|"
options:0
metrics:nil
views:views]];
}
When I run this method in an otherwise unedited project from the Master/Detail Xcode template, I get the following errors immediately on launch:
2014-08-21 13:51:02.141 TEST AutoLayout[2318:60b] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
2014-08-21 13:51:02.145 TEST AutoLayout[2318:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
And here is the full stack trace:
*** First throw call stack:
(
0 CoreFoundation 0x017f01e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0156f8e5 objc_exception_throw + 44
2 CoreFoundation 0x017f0048 +[NSException raise:format:arguments:] + 136
3 Foundation 0x0114f4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x0029ea38 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 567
5 libobjc.A.dylib 0x0158182b -[NSObject performSelector:withObject:] + 70
6 QuartzCore 0x03c5b45a -[CALayer layoutSublayers] + 148
7 QuartzCore 0x03c4f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
8 QuartzCore 0x03c5b3a5 -[CALayer layoutIfNeeded] + 160
9 UIKit 0x00360ae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
10 UIKit 0x00276aa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
11 UIKit 0x00275646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
12 UIKit 0x00275518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
13 UIKit 0x002755a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
14 UIKit 0x0027463a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
15 UIKit 0x0027459c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
16 UIKit 0x002752f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
17 UIKit 0x002788e6 -[UIWindow setDelegate:] + 449
18 UIKit 0x00352b77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
19 UIKit 0x0026e474 -[UIWindow addRootViewControllerViewIfPossible] + 591
20 UIKit 0x0026e5ef -[UIWindow _setHidden:forced:] + 312
21 UIKit 0x0026e86b -[UIWindow _orderFrontWithoutMakingKey] + 49
22 UIKit 0x002793c8 -[UIWindow makeKeyAndVisible] + 65
23 UIKit 0x00229bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
24 UIKit 0x0022e667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
25 UIKit 0x00242f92 -[UIApplication handleEvent:withNewEvent:] + 3517
26 UIKit 0x00243555 -[UIApplication sendEvent:] + 85
27 UIKit 0x00230250 _UIApplicationHandleEvent + 683
28 GraphicsServices 0x037e5f02 _PurpleEventCallback + 776
29 GraphicsServices 0x037e5a0d PurpleEventCallback + 46
30 CoreFoundation 0x0176bca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
31 CoreFoundation 0x0176b9db __CFRunLoopDoSource1 + 523
32 CoreFoundation 0x0179668c __CFRunLoopRun + 2156
33 CoreFoundation 0x017959d3 CFRunLoopRunSpecific + 467
34 CoreFoundation 0x017957eb CFRunLoopRunInMode + 123
35 UIKit 0x0022dd9c -[UIApplication _run] + 840
36 UIKit 0x0022ff9b UIApplicationMain + 1225
37 TEST AutoLayout 0x000025ed main + 141
38 libdyld.dylib 0x01e37701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I am fairly new to Auto Layout (perhaps quite obviously), so any broad explanation of my likely fundamental misunderstandings would be greatly appreciated!
Thanks!
While this doesn't directly answer your question about autoLayout, I find doing what you're looking to do in the following way is much simpler:
- (void)welcomeScreen {
UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
newView.backgroundColor = [UIColor redColor];
newView.autoresizingMask = UIViewAutoresizingFlexibleHeight + UIViewAutoresizingFlexibleWidth;
[self.view addSubview:newView];
}

Crash when installing the app on ipad, works fine in simulator

I am gettting the below issue in my application (Pls help me):
*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [1.07157e-08 nan]'
*** Call stack at first throw:
(
0 CoreFoundation 0x3126a64f __exceptionPreprocess + 114
1 libobjc.A.dylib 0x32b12c5d objc_exception_throw + 24
2 CoreFoundation 0x3126a491 +[NSException raise:format:arguments:] + 68
3 CoreFoundation 0x3126a4cb +[NSException raise:format:] + 34
4 QuartzCore 0x32c6f61d _ZL18CALayerSetPositionP7CALayerRKN2CA4Vec2IdEEb + 140
5 QuartzCore 0x32c6f58b -[CALayer setPosition:] + 38
6 QuartzCore 0x32c6f4d7 -[CALayer setFrame:] + 390
7 UIKit 0x35a31455 -[UIView(Geometry) setFrame:] + 188
8 UIKit 0x35a51743 UIViewCommonInitWithFrame + 594
9 UIKit 0x35a514a1 -[UIView initWithFrame:] + 84
10 CaCaCard 0x0000683b -[CaCaCardsAppViewController shouldAutorotateToInterfaceOrientation:] + 510
11 UIKit 0x35a6eead -[UIViewController _isSupportedInterfaceOrientation:] + 48
12 UIKit 0x35a6ee6f -[UINavigationController _doesTopViewControllerSupportInterfaceOrientation:] + 46
13 UIKit 0x35a6ee37 -[UINavigationController shouldAutorotateToInterfaceOrientation:] + 14
14 UIKit 0x35a6edbf -[UINavigationController _isSupportedInterfaceOrientation:] + 54
15 UIKit 0x35a6fbbb -[UIViewController window:shouldAutorotateToInterfaceOrientation:] + 46
16 UIKit 0x35a6f857 -[UIWindow _shouldAutorotateToInterfaceOrientation:] + 194
17 UIKit 0x35a6f4a3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 366
18 UIKit 0x35a6f0ad -[UIWindow setDelegate:] + 168
19 UIKit 0x35a6efa9 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 64
20 UIKit 0x35a6d611 -[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] + 460
21 UIKit 0x35a6d3e5 -[UINavigationController viewDidMoveToWindow:shouldAppearOrDisappear:] + 36
22 UIKit 0x35a4c5a7 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 506
23 UIKit 0x35a4c32f -[UIView(Hierarchy) _postMovedFromSuperview:] + 106
24 UIKit 0x35a31c1b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 678
25 UIKit 0x35a3196b -[UIView(Hierarchy) addSubview:] + 22
26 CaCaCard 0x00004bcf -[CaCaCardsAppAppDelegate application:didFinishLaunchingWithOptions:] + 318
27 UIKit 0x35a61821 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 772
28 UIKit 0x35a5bb65 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 272
29 UIKit 0x35a307d7 -[UIApplication handleEvent:withNewEvent:] + 1114
30 UIKit 0x35a30215 -[UIApplication sendEvent:] + 44
31 UIKit 0x35a2fc53 _UIApplicationHandleEvent + 5090
32 GraphicsServices 0x31d37e77 PurpleEventCallback + 666
33 CoreFoundation 0x31241a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
34 CoreFoundation 0x3124383f __CFRunLoopDoSource1 + 166
35 CoreFoundation 0x3124460d __CFRunLoopRun + 520
36 CoreFoundation 0x311d4ec3 CFRunLoopRunSpecific + 230
37 CoreFoundation 0x311d4dcb CFRunLoopRunInMode + 58
38 UIKit 0x35a5ad49 -[UIApplication _run] + 372
39 UIKit 0x35a58807 UIApplicationMain + 670
40 CaCaCard 0x000028e7 main + 70
41 CaCaCard 0x0000289c start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/Developer/usr/lib/libXcod
eDebuggerSupport.dylib (file not found).
(gdb)
-(void) viewWillAppear:(BOOL)animated
//-----------------------------------
{
if ( self.interfaceOrientation == UIInterfaceOrientationPortrait ||
self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
[NSThread sleepForTimeInterval:0.5];
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[[UIApplication sharedApplication] setStatusBarOrientation:
UIInterfaceOrientationLandscapeRight];
UIScreen *screen = [UIScreen mainScreen];
CGFloat screenWidth = screen.bounds.size.width;
CGFloat screenHeight = screen.bounds.size.height;
UIView *navView = [[self navigationController] view];
navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
navView.transform = CGAffineTransformIdentity;
navView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
[UIView commitAnimations];
}
fontSelected = NO;
}
The problem is an invalid frame size you are setting at some point. Probably a rect with a zero value somewhere.
From your code I would suspect you are getting some unexpected values back when you are asking for the screen size.
Also don't use those old animation functions. Use the block based animation. It's much better.

Resources