How can I override auto layout (in UICollectionView) - objective-c

I have an UICollectionView and a custom UICollectionViewCell containing subviews.
I am doing some "esoteric" stuff inside this CollectionViewCell and would like do rearrange
all subviews by using setFrame manually. How can I override all autolayout behaviour?
I tried to override "-(void) layoutSubviews" and draw rect. Sometimes this works, sometimes not.
If I get a cell by using
UIMyCell *cell = (UIMyCell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"UIMyCell" forIndexPath:indexPath];
I get my custom cell. I can assign custom properties and everything is fine. But the frame of this cell is initially (0, 0, 0, 0).
Some time later the layout will be set. I would like to be "notified" if the subsystem is setting the size.
Then I would like to set all subviews frames WITHOUT autolayout.
What is the best practice here?

Finally I found the answer.
On OS X (NSView)
-(void) setFrame:(CGRect)frame
will be called after all constraints have been updated to set new view positions and sizes.
On iOS (UIView)
-(void) setCenter:(CGPoint)center
and
-(void)setBounds:(CGRect)bounds
will be called instead. So overwriting setFrame is not working for iOS framework.

Related

MKMapView defaults to 1000x1000 in Xcode8

I'm wondering if anyone else has the issue that MKMapView (Or maybe it relates to many other Views?) does a strange thing where it defaults to 1000x1000 in size the first time I load a screen. When I segue to another screen, and segue back to the map screen, then it actually followed up all my constraints.
This didn't happen with Xcode7 and I wonder if someone knows there's a known bug in Xcode8 or something?
MapKitView get loaded programmatically and is added as a subview within my View that has the constraints.
MKMapView *mapkitView = [[MKMapView alloc] initWithFrame:self.aConstrainedView.frame];
[self.mapkitView setDelegate:self];
[self.mapkitView setShowsUserLocation:YES];
[self.mapkitView setRotateEnabled:NO];
[self.aConstrainedView addSubview:self.mapkitView];
Could this be related to Autoresizing issue in Xcode 8 ?
Your initialiser should use the bounds of its superview, not the frame:
MKMapView *mapkitView = [[MKMapView alloc] initWithFrame:self.aConstrainedView.bounds];
Set a breakpoint on this line and in the debugger use:
(lldb) po self.aConstrainedView.bounds
to ensure the width and height are correct. If you're adding the map view in viewDidLoad, then that's too early. You should override:
- (void)viewDidLayoutSubviews {
dispatch_once(
// Add map view here
);
}
and add your code in there, as that gets called after your subviews have all been laid out according to your constraints.

Asynchronously loading image into autolayout UIImageView

I've searched SO and have not seen an answer specifically for Autolayout.
I have a UITableViewCell using Autolayout. The UIImageView is constrained to have its edges 12 pt from Cell edges and the trailing edge of the Label.
Images are retrieved asynchronously and assigned to the UIImageView's image property.
[self downloadImageWithURL:[NSURL URLWithString:imageUrlString] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
weakSelf.newsImageView.image = image;
// [weakSelf.contentView layoutIfNeeded];
}
}];
If there is a blank placeholder image assigned to the image of the cell in cellForRowAtIndexPath, then the asynchronously retrieved image is loaded and visible. If the blank placeholder assignment is commented out, the asynchronously retrieved image never appears.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];
// cell.newsImageView.image = _blankImage;
return cell;
}
I tried layoutIfNeeded (commented out above) and updateConstraintsIfNeeded to see if the image would appear but it did not work. So for now I've been putting a blank image there first, which seems like a hack, and wouldn't be flexible if relying on intrinsic content size of images who's sizes could be different. Are the constraints being optimized out of existence when the first layout occurs? Anyone know what's going on and how to resolve it?
Try calling: invalidateIntrinsicContentSize on your UIImageView in the completion handler. This should force the auto layout system to recalculate the view intrinsic size. Possibly, you could also need calling updateConstraintsIfNeeded after that (but try first without).
If this does not work and your UIImageViews have a known size, you could try subclassing UIImageView and override the -intrinsicContentSize method. Return the known size from it.
Turns out the nib somehow had two outlets referring to the same UIImageView element. One outlet didn't exist in code, yet still linked in the nib UI. Must have been a mishap in refactoring, but removing it fixes it.

Resizing UICollectionViewCell After Data Is Loaded Using invalidateLayout

This question has been asked a few times but none of the answers are detailed enough for me to understand why/how things work. For reference the other SO questions are:
How to update size of cells in UICollectionView after cell data is set?
Resize UICollectionView cells after their data has been set
Where to determine the height of a dynamically sized UICollectionViewCell?
I'm using MVC but to keep things simple lets say that I have a ViewController that in ViewWillAppear calls a web service to load some data. When the data has been loaded it calls
[self.collectionView reloadData]
The self.collectionView contains 1 UICollectionViewCell (let's call it DetailsCollectionViewCell).
When self.collectionView is being created it first calls sizeForItemAtIndexPath and then cellForItemAtIndexPath. This causes a problem for me because it's only during cellForItemAtIndexPath that I set the result of the web service to DetailsCollectionViewCell via:
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"detailsCell" forIndexPath:indexPath];
((DetailsCollectionViewCell*)cell).details = result;
DetailsCollectionViewCell has a setter for the property details that does some work that I need to happen first to know what the correct cell size should be.
Based on the linked questions above it seems like the only way to fire sizeForItemAtIndexPath after cellForItemAtIndexPath is to call
[self.collectionView.collectionViewLayout invalidateLayout];
But this where the other questions don't work for me because although it calls sizeForItemAtIndexPath and allows me to grab enough information from DetailsCollectionViewCell to set the correct height it doesn't update the UI until after the user scrolls the UICollectionView and my guess is that it has something to do with this line from the documentation
The actual layout update occurs during the next view layout update cycle.
However, i'm stumped on how to get around this. It almost feels like I need to create a static method on DetailsCollectionViewCell that I can pass the web service result to during the first sizeForItemAtIndexPath pass and then just cache that result. But i'm hoping there is a simple solution to having the UI automatically update instead.
Thanks,
p.s. - First SO question so hope i followed all the rules correctly.
Actually, from what I found, calling to invalidateLayout will cause calling sizeForItemAtIndexPath for all cells when dequeuing next cell (this is for iOS < 8.0, since 8.0 it will recalculate layout in next view layout update).
So the solution i came up with, is subclassing UICollectionView, and overriding layoutSubviews with something like this:
- (void)layoutSubviews
{
if ( self.shouldInvalidateCollectionViewLayout ) {
[self.collectionViewLayout invalidateLayout];
self.shouldInvalidateCollectionViewLayout = NO;
} else {
[super layoutSubviews];
}
}
and then calling setNeedsLayout in cellForItemAtIndexPath and setting shouldInvalidateCollectionViewLayout to YES. This worked for me in iOS >= 7.0. I also implemented estimated items size this way. Thx.
Here my case and solution.
My collectionView is in a scrollView and I want my collectionView and her cells to resize as I'm scrolling my scrollView.
So in my UIScrollView delegate method : scrollViewDidScroll :
[super scrollViewDidScroll:scrollView];
if(scrollView.contentOffset.y>0){
CGRect lc_frame = picturesCollectionView.frame;
lc_frame.origin.y=scrollView.contentOffset.y/2;
picturesCollectionView.frame = lc_frame;
}
else{
CGRect lc_frame = picturesCollectionView.frame;
lc_frame.origin.y=scrollView.contentOffset.y;
lc_frame.size.height=(3*(contentScrollView.frame.size.width/4))-scrollView.contentOffset.y;
picturesCollectionView.frame = lc_frame;
picturesCollectionViewFlowLayout.itemSize = CGSizeMake(picturesCollectionView.frame.size.width, picturesCollectionView.frame.size.height);
[picturesCollectionViewFlowLayout invalidateLayout];
}
I had to re set the collectionViewFlowLayout cell size then invalidate his layout.
Hope it helps !

Resize UICollectionView cells after their data has been set

My UICollectionView cells contain UILabels with multiline text. I don't know the height of the cells until the text has been set on the label.
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
This was the initial method I looked at to size the cells. However, this is called BEFORE the cells are created out of the storyboard.
Is there a way to layout the collection and size the cells AFTER they have been rendered, and I know the actual size of the cell?
I think your are looking for the invalidateLayout method you can call on the .collectionViewLayout property of your UICollectionView. This method regenerates your layout, which in your case means also calling -collectionView: layout: sizeForItemAtIndexPath:, which is the right place to reflect your desired item size. Jirune points the right direction on how to calculate them.
An example for the usage of invalidateLayout can be found here. Also consult the UICollectionViewLayout documentation on that method:
Invalidates the current layout and triggers a layout update.
Discussion:
You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.
Edit:
For storyboard collection view which contains auto layout constraints, you need to override viewDidLayoutSubviews method of UIViewController and call invalidateLayout collection view layout in this method.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[yourCollectionView.collectionViewLayout invalidateLayout];
}
subclass UICollectionViewCell and override layoutSubviews like this
hereby you will anchor cell leading and trailing edge to collectionView
override func layoutSubviews() {
super.layoutSubviews()
self.frame = CGRectMake(0, self.frame.origin.y, self.superview!.frame.size.width, self.frame.size.height)
}
Hey in the above delegate method itself, you can calculate the UILabel size using the below tricky way and return the UICollectionViewCell size based on that calculation.
// Calculate the expected size based on the font and
// linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(9999,9999);
CGSize expectedLabelSize =
[[self.dataSource objectAtIndex:indexPath.item]
sizeWithFont:[UIFont fontWithName:#"Arial" size:18.0f]
constrainedToSize:maximumLabelSize
lineBreakMode:NSLineBreakByWordWrapping];
- (void)viewDidLoad {
self.collectionView.prefetchingEnabled = NO;
}
In iOS 10, prefetchingEnabled is YES by default. When YES, the collection view requests cells in advance of when they will be displayed. It leads to crash in iOS 10

Preload cells of uitableview

I acknowledge that UITableview load dynamically a cell when user scrolls. I wonder if there is a way to preload all cells in order not to load each one while scrolling. I need to preload 10 cells. Is this possible?
You can initialize table view cells, put them into an array precomputedCells and in the data source delegate method tableView:cellForRowAtIndexPath: return the precomputed cells from the array instead of calling dequeueReusableCellWithIdentifier:. (Similar to what you would do in a static table view.)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self.precomputedCells objectAtIndex:indexPath.row];
}
It might also be useful to have a look at the
WWDC 2012 Session 211 "Building Concurrent User Interfaces on iOS"
where it is shown how to fill the contents of table view cells in background threads while keeping the user interface responsive.
I was looking for a solution to the original question and I thought I'd share my solution.
In my case, I only needed to preload the next cell (I won't go into the reason why, but there was a good reason).
It seems the UITableView renders as many cells as will fit into the UITableView frame assigned to it.
Therefore, I oversized the UITableView frame by the height of 1 extra cell, pushing the oversized region offscreen (or it could be into a clipped UIView if needed). Of course, this would now mean that when I scrolled the table view, the last cell wouldn't be visible (because the UITableView frame is bigger than it's superview). Therefore I added an additional UIView to the tableFooterView of the height of a cell. This means that when the table is scrolled to the bottom, the last cells sits nicely at the bottom of it's superview, while the added tableFooterView remains offscreen.
This can of course be applied to any number of cells. It should even be possible to apply it to preload ALL cells if needed by oversizing the UITableView frame to the contentSize iOS originally calculates, then adding a tableFooterView of the same size.
Hopefully this helps someone else with the same problem.
As suggested by Mark I also changed the height of my UITableView temporarily so that the table view creates enough reusable cells. Then I reset the height of my table view so that it stops creating reusable cells while scrolling.
To accomplish that I create a helper bool which is set to false by default:
var didPreloadCells = false
It is set to true when my table view first reloaded data and therefore created the first reusable cells.
resultsHandler.doSearch { (resultDict, error) -> Void in
[...]
self.tableView.reloadData()
self.didPreloadCells = true
[...]
}
The real trick happens in my viewDidLayoutSubviews Method. Here I set the frame of my table view depending on my boolean. If the reusable cells were not created yet I increase the frame of the table view. In the other case I set the normal frame
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.tableView.frame = self.view.bounds
if !didPreloadCells
{
self.tableView.frame.size.height += ResultCellHeight
}
}
With the help of that the table view creates more initial reusable cells than normal and the scrolling is smooth and fluent because no additional cells need to be created.
Change the dequeueReusableCellWithIdentifier when you are reusing the table view otherwise it will load the old data
Solution for autoresizing cells. Change 'estimatedRowHeight' to lower value
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 32; //Actual is 64
}
Actual estimated height is 64. 32 is used to add more cells for reuse to avoid lagging when scrolling begins