UICollectionViewFlowLayout undefined when I test in devices with smaller screens - ios

To the point:
This is happening only when I test in smaller screen sizes because looks like the cell size fits only with iphone 6+.
What I would like to know is how to set a size that fix with all devices?
Details:
I know this question existshere and here but I've tried everything said in the first question and in the second one it's different because they only manage to solve a specific problem that happens when they change from portrait to landscape and the size of the cells changes. In my case my app is portrait and the size of the cell is not set to be bigger than it's container in storyboard, as you can see in the pictures. But look like the size only fit with an iphone 6+ screen and when I use a smaller device I have this warning:
the behavior of the UICollectionViewFlowLayout is not defined because:
2016-08-25 12:27:12.719 xxx[2722:761734] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2016-08-25 12:27:12.720 xxx[2722:761734] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x15a018930>, and it is attached to <UICollectionView: 0x156994000; frame = (14 287.5; 292 45); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x159dd0020>; layer = <CALayer: 0x159d55dd0>; contentOffset: {0, 0}; contentSize: {572, 0}> collection view layout: <UICollectionViewFlowLayout: 0x15a018930>.
2016-08-25 12:27:12.720 xxx[2722:761734] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
I have set a symbolic breakpoint but the only thing I see is this:
My cell size and collection view size is here :
The size of the cell is equal or smaller in all the cases. The only thing I change in code is the height of the collection view that contains the cells. And I set its size bigger, to allow more cells in the case there are more cells to display.
What I would like to know is how to set the correct values of the UICollectionViewCell size for each device, and what it cannot be done in storyboard??
Thanks a lot

Related

iOS UICollectionView ConsoleLog Caution when Item Width is bigger than CollectionView width

I'm using UICollectionView for my project and I put bigger Item than collectionView width to hide selectionButton/DeleteButton and show them when Edit mode or Swiped
Everything is working good but there's caution message in ConsoleLog EveryTime
As I know, there're some Messages I can ignore But I want to know if it's Okay to ignore it
Is it okay to put bigger width for item than collectionView width and ignore that Message?
I don't want you to waste your time with reading every Single line of my code and fix for me
So, I just hope to hear your Idea and if there're some points I should be careful when I using this Way :)
2020-11-01 19:08:14.917103+0900 IssueTracker[27304:7532603] The behavior of the UICollectionViewFlowLayout is not defined because:
2020-11-01 19:08:14.917210+0900 IssueTracker[27304:7532603] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2020-11-01 19:08:14.917241+0900 IssueTracker[27304:7532603] Please check the values returned by the delegate.
2020-11-01 19:08:14.917601+0900 IssueTracker[27304:7532603] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x10571ae80>, and it is attached to <UICollectionView: 0x10a811800;
frame = (0 172; 414 515); clipsToBounds = YES;
autoresize = RM+BM;
gestureRecognizers = <NSArray: 0x2829d5290>;
layer = <CALayer: 0x282780ea0>;
contentOffset: {0, 0};
contentSize: {414, 101};
adjustedContentInset: {0, 0, 0, 0};
layout: <UICollectionViewFlowLayout: 0x10571ae80>;
dataSource: <IssueTracker.IssueListCollectionViewAdapter: 0x28279a2c0>>.
I think that the best way to implement it is by disabling buttons and animating their appearance by changing alpha of text colors. I implemented it this way in my video application and it works perfectly!

Full-screen UICollectionViewCell issue on iPhone X in landscape

In my app, I present a view controller modally, and this view controller contains a UICollectionView that fills the superview. Each cell in the collection view needs to be full-screen - the same size as the view's bounds. To accomplish this, I simply set the itemSize of the UICollectionViewFlowLayout to be the view's bound's size in viewDidLayoutSubviews.
This works really well on all devices and orientations, except iPhone X in landscape. When presenting that screen in that case, it seems the safe area layout margins are coming into play and affecting positioning of the cell. The cell is pushed over from the left edge of the display, and pushed up such that the bottom of the cell is aligned to the vertical center of the home indicator. Inspecting the layout with View Debugger reveals the collection view fills the screen, but the cell's Y position is -10.67 and the X position is 0, even though it is inset from the leading of the collection view. Notably, this is logged in the console:
The behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa6311253b0>, and it is attached to <UICollectionView: 0x7fa63207e800; frame = (0 0; 812 375); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x608000244d70>; layer = <CALayer: 0x60800023ef00>; contentOffset: {-44, 0}; contentSize: {0, 812}; adjustedContentInset: {0, 44, 21, 44}> collection view layout: <UICollectionViewFlowLayout: 0x7fa6311253b0>.
What's going on here and how can this be resolved?
By default, iOS 11 automatically adjusts content insets for iPhone X in landscape, causing this issue to occur. To resolve it, you can simply disable the automatic content inset adjustment, as it's not needed nor desired in this scenario.
collectionView.contentInsetAdjustmentBehavior = .never

EXC_ARITHMETIC in UIFlowLayoutSection.updateEstimatedSizeForSection

I get the following error in simulator when trying to display a UICollectionView:
UIKit`-[_UIFlowLayoutSection updateEstimatedSizeForSection:]:
...
0x106d32858 <+582>: movq 0x416909(%rip), %rcx ; _UIFlowLayoutSection._validItemRange
0x106d3285f <+589>: xorl %edx, %edx
-> 0x106d32861 <+591>: divq 0x8(%rcx,%rbx)
Thread 1: EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)
I have set the estimated cell size correctly and also set the collection view height bigger than the cell height. All insets have been set to 0. But before the crash I still get the following warnings:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
Please check the values return by the delegate.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fe65d28b7c0>, and it is attached to <UICollectionView: 0x7fe65c859000; frame = (5 63; 365 110); clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x7fe65d28bea0>; animations = { opacity=<CABasicAnimation: 0x7fe65d702fd0>; bounds.origin=<CABasicAnimation: 0x7fe65d704610>; bounds.size=<CABasicAnimation: 0x7fe65d704630>; }; layer = <CALayer: 0x7fe65d03a0b0>; contentOffset: {0, 0}; contentSize: {1798, 110}> collection view layout: <UICollectionViewFlowLayout: 0x7fe65d28b7c0>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
Any ideas?
I fixed this by setting the UIImageView size to fixed in the collection view cell instead of having constraints to stretch the image view to the size of the cell.
This is weird because UICollectionView should not scale the cell to larger than the size returned in the dataSource. Also, what has the UIImageView size got to do with the cell size when it has clipsToBounds = true? UICollectionView shouldn't even know what views the cell contains as long as the cell itself has the correct size.

Springs and struts resizing broken on iOS 8 UITableViewCell

I'm trying to set up a simple UITableViewCell in Interface Builder without auto layout. I'm using the old springs and struts.
When I give a label a flexible width, it seems to lay out as if the UITableViewCell has a much larger width, even though the UITableViewCell tells me it has a width of 375 pixels in layoutSubviews:
- (void)layoutSubviews
{
[super layoutSubviews];
NSLog(#"Label width: %f", self.nameLabel.frame.size.width); // 695.0 for a label that stretches the whole width
NSLog(#"Superview (UITableViewCell) width: %f", self.nameLabel.superview.frame.size.width); // 375.0
}
On a simulated iPhone 5S (iOS 7 or 8), the superview is 320 but the UILabel spreads to 640.
On a simulated iPhone 6, the superview is 375 but the UILabel spreads to 695.
On a simulated iPhone 6 Plus, the superview is 414 but the UILabel speads to 734.
I don't have this problem with other views. For example, I'm able to add a UILabel to a UIViewController and have it stretch the width correctly. What is going on? And how do I fix this?
EDIT:
Interestingly, if I add the constraints programmatically during cellForRowAtIndexPath: then it seems to work as expected, so long as I use the older dequeueReusableCellWithIdentifier instead of dequeueReusableCellWithIdentifier:forIndexPath:. I want to keep all my constraints in Interface Builder though. Any ideas?
I played around a bit with this today. Looks like the frame of the label when it's instantiated relative to the frame of the content view is wrong. If I make the label the same size as the cell in the storyboard, then in awakeFromNib the contentView has a size of CGRectZero but the label has the size I set in the storyboard. Therefore, when you get to layoutSubviews and the contentView is resized to the right size (0,0,320,44), because of the flexible width mask, the label itself is resized along with the content view (width increases by 320 as well). That's why its appearing larger than intended.
The only way I could get around this (albeit, it feels like a gross hack and you should probably stick to auto-layout), was to set the label's frame relative to the content view in awakeFromNib.
- (void)awakeFromNib
{
[super awakeFromNib];
self.label.frame = self.bounds;
}

Set the width of an grouped cell in Xcode

Am fooling around with this question a couple of days now but no progress. What i want to do is quite simple i think:
I have an image of 320x60 which i use in the plain TableView which works oke as those cells take up the entire width (320) of the screen. The grouped cells in a TableView are 300 wide and have insets/margins left of 10 on the left and the right.
Can i somehow remove those insets/margins and let the grouped cell be 320 wide? I tried setting the content inset left to -10. That does "remove" the left margin but then it's still only 300 wide. Also tried editing the XML of the storyboard (I'm working with iOS 5 - Storyboards) but no joy.
This similar question here got answered as no it's not possible, hopfully something changed in 2+ years!:
Adjust cell width in grouped UITableView
PS i want to alter the width as the background images contain nice shadows, I've read that exesive use of shadows could mean performance issues. Also the shadow's are 5px extra around the border so that would mean -10px wide if I use the standard width.
Help much appreciated!
An untidy solution is to make the table view 340 pixels wide, and 10 pixels off the left edge of the screen.
A solution that involves changing properties of private classes is to make a UITableViewCell subclass, and override its layoutSubviews method. When I log the subviews, I find these:
"<UIGroupTableViewCellBackground: 0x95246b0; frame = (9 0; 302 45); autoresize = W; layer = <CALayer: 0x95226b0>>",
"<UITableViewCellContentView: 0x92332d0; frame = (10 0; 300 43); layer = <CALayer: 0x9233310>>",
"<UIView: 0x95248c0; frame = (10 0; 300 1); layer = <CALayer: 0x951f140>>"
What happens if we take those subviews and fill the entire bounds available?
- (void)layoutSubviews;
{
// By default the cell bounds fill the table width, but its subviews (containing the opaque background and cell borders) are drawn with padding.
CGRect bounds = [self bounds];
// Make sure any standard layout happens.
[super layoutSubviews];
// Debugging output.
NSLog(#"Subviews = %#", [self subviews]);
for (UIView *subview in [self subviews])
{
// Override the subview to make it fill the available width.
CGRect frame = [subview frame];
frame.origin.x = bounds.origin.x;
frame.size.width = bounds.size.width;
[subview setFrame:frame];
}
}
At this particular moment, on the iOS 5.1 simulator, this works. Now, some future version of iOS may restructure these classes, causing this method to catastrophically mangle the table layout. Your app could be rejected for changing the properties of UITableViewCellContentView... even though you're only modifying its frame. So, how much do you need to have your cells fill the table width?
You can the UITableView's Leading and Trailing Space constraints in the Size Inspector which is accessible via the Storyboard. I'm not sure when this was added, but setting the Leading Space Constraint to -10 and the Trailing Space Constraint to 10 will make the cells full width.

Resources