I'm working on an app that has a MKMapView with annotations. Everything works fine on iOS7, but when I try it on iOS6, as I tap on an annotation, the app crashes with :
*** Assertion failure in -[MyAnnotationView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5776
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. MyAnnotationView's implementation of -layoutSubviews needs to call super.'
I've added :
- (void)layoutSubviews
{
[super layoutSubviews];
}
to my MyAnnotationView, but nothing changes. I don't really know what to do here.
Thanks for your help !
EDIT :
I am using auto layout. I've tried to add
self.translatesAutoresizingMaskIntoConstraints = NO;
in MyAnnotationView init method, but nothing changes. (I've tried with YES too)
Related
My application crashes in ios7 when i add a placeholder to text field ,the same works fine in ios8. Added controls programmatically with auto layout
As suggested i added [self.view layoutIfNeeded]; at the end of viewDidLayout delegate method but still the app crashed with the below exception
Assertion failure Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITextField's implementation of -layoutSubviews needs to call super.'
It is very important where you add your constraints programmatically. You should not add them in one of the layouting methods callbacks (like layoutSubviews or viewdidLayoutSubviews). That is because the system is expected to have the views layout after these methods are called and if you add new constraints after the views were already layouted and a new layout cycle is needed, there will be a crash.
I suggest you think about another method where you could add the new constraints. If there is no other place that would work for you, try adding the constraints before you call the super method, not the other way around.
So something like this:
- (void)viewDidLayoutSubviews {
// Add constraints
[super viewDidLayoutSubviews];
}
If you could post some of your code, I would me more than happy to help you further :)
I created UITextField in storyboard. And added its constraints also. I want search icon on left side of UITextField. The code for adding search icon is as follows:
self.searchTextField.leftView = searchIconImage;
self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
[self.searchTextField addTarget:self
action:#selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
My application is working fine on iOS8 and its crashing on iOS7. The error is as follows:
Assertion failure in -[UITextField layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.138/UIView.m:8794
2014-11-05 12:54:33.377 WattUp[1722:60b] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITextField's implementation of -layoutSubviews needs to call super.'
So I ran into this same error a few days ago as well. It turns out I was trying to layout subviews inside my UITextfield subclass, setting properties on them, moving them, etc, but was never explicitly telling the view to lay itself out (i.e. calling [self layoutIfNeeded]).
iOS 8 seems to force to a view to layout all its subviews, and then configures constraints on it. iOS 7 won't, and needs you to explicitly tell views to redraw their subviews when you change if you're using autolayout.
In my case, I had subclassed UITextField and added a label to the side. I configured the frame of the label by adding constraints to the UITextfield. One of the public methods I could call on my class was
- (void)setLabelText:(NSString *)newText{
self.sideLabel.text = newText;
}
This caused my application to crash when a view controller appeared containing my subclassed textfield. By add layoutIfNeeded everything works fine in iOS7 and iOS8.
- (void)setLabelText:(NSString *)newText{
self.sideLabel.text = newText;
[self layoutIfNeeded];
}
This needs to be called every time you change a part of the view in your subclass. This includes the setup when you add subviews, when you change view properties, anything really. Before the function that's changing your view returns, call layoutIfNeeded on your view. This seems to apply for a few standard UI controls including UITextfield, UITableView and UICollectionView, though I'm sure there are others. I hope this was clear enough and helped solve your problem.
The error you're getting isn't super useful, and didn't even apply in my case. Though I was receiving the exact same error, none of my views implementing layoutSubviews, and thus were all using the [super layoutSubviews] method.
I solved this by subclassing UITextField and reimplementing 2 methods:
- (void)setText:(NSString *)text
{
[super setText:text];
[self layoutIfNeeded];
}
- (void)layoutSubviews
{
[super layoutSubviews];
}
really annoing thing. Apple makes programmers cry so often
I put UISearchBar under UIView as the picture below. The problem is - after finish search or click cancel, UISearchBar will shift to the top, then I get error message:
2014-05-22 15:45:18.663 MyAPP[14402:60b] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
2014-05-22 15:45:18.667 MyAPP[14402:60b] WARNING: GoogleAnalytics 3.06 void GAIUncaughtExceptionHandler(NSException *) (GAIUncaughtExceptionHandler.m:49): Uncaught exception: Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.
2014-05-22 15:45:23.697 MyAPP[14402: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.'
*** First throw call stack:
Question:
1) Is it possible to fix position of UISearchBar? and How to do that?
2) How to fix the error?
I get an error saying
*** Assertion failure in -[HatsViewController loadView], /SourceCache/UIKit/UIKit-2380.17/UICollectionViewController.m:104
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "FCD-ki-UFO-view-1vb-8P-s3C" nib but didn't get a UICollectionView.'
*** First throw call stack:
(0x3467c2a3 0x3c36097f 0x3467c15d 0x34f51ab7 0x368aaa95 0x364a346d 0x3652f353 0x36570d11 0x3656ffe7 0x366923ef 0x3656f0c5 0x3656f077 0x3656f055 0x3656e90b 0x3656ee01 0x364975f1 0x36484801 0x3648411b 0x3818b5a3 0x3818b1d3 0x34651173 0x34651117 0x3464ff99 0x345c2ebd 0x345c2d49 0x3818a2eb 0x364d8301 0xc0dfd 0x3c797b20)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Why does this happen and how can I fix it?
When you’re using a UICollectionViewController, the view outlet needs to be connected to a UICollectionView in your storyboard; if it’s a UIView of another class, it’ll crash.
check HatsViewController.h is inherited from UICollectionViewController but you have connected it view in your storyboard. Either it must be subclass of UIViewController or it is connected to UICollectionView in your storyboard.
I had my LayoutConstraints working fine, then all of a sudden I started getting this when adding Constraints to my view.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Constraint must contain a first layout item'
*** First throw call stack:
(0x7cd012 0x2017e7e 0x7ccdeb 0xde6bf1 0x9c487 0x994a3 0x414fb 0x20224 0x5c6c0 0xc30e83 0x78c376 0x78be06 0x773a82 0x772f44 0x772e1b 0x28eb7e3 0x28eb668 0x149465c 0x2cfd 0x2c25)
libc++abi.dylib: terminate called throwing an exception
And there exists no immediate documentation on the internet or on the Apple Docs as to what the error message means.
Just wondering if anyone has encountered this error, and know what I can do to troubleshoot it?
One more reason for this error/crash can be, the views passed to the [NSLayoutContraint constraintWithItem ...] method are nil.
Ah, found the issue.
For anyone who has this issue, it's because the view hasn't been created in the nib/UIView yet, so no constraints can apply.
I moved the Constraint code from the initWithNib method to viewDidLoad and the error naturally stopped occurring.
If you're hanging on to your views with properties make sure they're strong! This could account for them being nil.
This can also happen if you switch off Autolayout for some reason, but you're still adding constraints to any subview via code (e.g. for handling orientation changes)
This happened to me when I used to different NIB's for the iPhone and iPad and switched off Autolayout for only the iPhone NIB while using a common .m file.
Solution is to check for the device type and skip the layout addition where it's not needed.
This happened when I accidentally deleted the 'referencing outlet' for one of my UI elements.