Height of the collection view is getting calculated wrongly while using tabbar controller in iOS 10 - ios

I have created a tab-bar controller in which a sample view controller is present with a container view. The container is embedded with referencedStoryBoard.
The reference storyboard contains a view controller within which collectionView controller is embedded.
When my app runs the collectionView cell height is calculated wrongly. The collection view cell is getting hidden on the top.
Note:
1)Collection view scroll direction is horizontal. On vertical side its working good
2) When I am not using the tabBar the collectionView cell height is getting calculated correctly.
You can find the sample project in this link: https://drive.google.com/open?id=1GbKC_ZhqQAlcLnD24YgxQTZt2toVprwF
The issue is working good in iOS11.

Problem (In my opinion):
At start-up SampleCollectionViewController is created with a normal size (maybe fullscreen size). At this time, collectionView is created and reload. Size of collectionViewCell is calculated base on size of SampleCollectionViewController.
But after that, SampleCollectionViewController is embedded in another controller with a smaller size. Size of SampleCollectionViewController change but collectionView isn't reloaded and collectionViewCell keep start-up size.
Now height of collectionViewCell is bigger than size of SampleCollectionViewController on screen. That's why top of the cell is hidden.
Solution: Call invalidateLayout in traitCollectionDidChange: method. It means that when size of SampleCollectionViewController is changed, calculate cell size and update cells with right size.
SampleCollectionViewController.m
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self.collectionView.collectionViewLayout invalidateLayout];
}
Project works normally on my side.

Select your view controller in storyboard. Check if adjust scroll view insets is selected, then unselect it. Thats how i fixed my problem.

When I change the design, I am able to fix that issue for iOS 10. Also working good on iOS 11.
Instead of container view for storyboard reference, I created a segue to call the storyboard reference. As issue is specific to iOS 10 and horizontal collection view, I am gonna continue with earlier design as mentioned in the question.

Related

Cannot change custom UICollectionViewCell in Storyboard

I have a UICollectionView with custom UICollectionViewCells. I used Interface Builder (via XCode 8) to size the collection view and cells when first making it.
Now, I'm trying to adjust the size of the cells and I'm unable to do so in Interface Builder (using XCode 9). I cannot adjust the width or height of the cells on either the collection view itself or on the custom cell. The up/down arrows don't do anything and neither does typing in a value. It just stays set at 145.
I saw some answers about not being able to do this with a UICollectionViewController via Interface Builder, only a UICollectionView, but it is a collection view and not UICollectionViewController.
I'm sure there's some new setting/config in XCode 9 that I'm missing, but I can't figure out why I can't adjust the custom value.
I ran into the same problem.
Turned out that the item size was defined by the contained flow layout. So editing the item size in the flow layout, and the parent collection view had its item size changed accordingly.
I tried Jonny's answer, but I use a custom FlowLayout and couldn't edit it's cell size either. I had to manually edit the size of the collection view, the cell and the layout in the storyboard source code (right-click on the storyboard in project view and select "open as / source code").
Now the size is correct but still not editable from the UI ¯_(ツ)_/¯
Same problem, width would not change. But I changed the height to something different and it then let me edit the width.
Xcode 12.3. I had the same issue and what fixed it for me was changing the collection view scroll direction from vertical to horizontal to make the change, then obviously reverting back to vertical (my intended direction) once I was done editing the values.

How to give constraints to a tableview when tableview cell is on xib. And table view is on storyboard

I have a table view on storyboard i.e. FirstStory.Storyboard. And for this tableview I have created a table view cell on xib file where I give size to its width as per my current storyboard width. But when I launch my app it runs fine on the landscape position. But when I rotate it in the portrait mode. Width is unable to show the all item which I placed in tableviewcell.xib. How we mutually combine these two item in storyboard (table view on storyboard while cell on xib). Which follow a single constraints with above condition.
UITableViewCell's contentView property is always the size of the cell. So you have to setup the constraints of your UI components in the xib file with the Cell's contentView properly. The issue you are facing must be because you did not setup these constraints properly.
To see which views or constraints have been placed wrong, use the XCode's 'Debug View Hierarcy' feature.
Launch it from here
Run your app, navigate to your tableView, then press the above button to launch the view debugger. It provides very good insight into your constraints and where the issue may lie

UITableview with Autolayout and NSFetchedResults controller cell layout bug on insert

I seem to be having a very strange problem with my UITableview cells and autolayout. So my tableview is set up as follows:
I have a UITableView within a UIViewController that I created via Interface builder using Autolayout
My tableview is using UITableViewAutomaticDimension so that the cells can resize based on the text within them.
Each cell has 2 subviews within it. A front view and a back view. The back view contains the buttons for my slide-able cells
I am using NSFetchedResults controller to update the tableview
The problem occurs when I tap on a cell and am taken to the next view. Then I create a core data item within that view and save so that it is inserted into my tableview (now behind the current view controller).
What happens is that the newly inserted cell seems to lose all autolayout constraints and all view elements are on top of each other in the top left of the cell. It also looks like they are not even contained within the front view for the cell (or the front view has a width and height of 0).
Example:
It gets even stranger. If I refresh the tableview by pulling down and the cells are reloaded it corrects itself. However, if I refresh the tableview again then the problem appears again but on a different cell. This bug does not happen consistantly which is rather frustrating.
Any help would be appreciated as I only have a few remaining hairs on my head to pull out at this stage.
Thanks in advance!
So I found out what the issue was. The issue was that I was using size classes in Interface builder but I was only using the iPhone Portrait size class and did all my work in there instead of the "any" size class. Disabling size classes and setting the project to iPhone only fixed all these problems.
Weird I know

Difference in constraints iOS 7 and iOS 8. Xcode 6 seed

So I have a UIViewController with a UIImageView and a Collection View, as well as a Navigation Bar. This app is only in portrait mode and won't change when rotating.
in iOS 7.1.2 and below, it shows up correctly, like this:
image1 http://imageshack.com/a/img911/4185/koGDtt.jpg
In iOS 8, it shows up incorrectly where the collection view is at the very top of the screen behind the navigation bar, like this:
image2 http://imageshack.com/a/img673/2531/NWYWsn.jpg
I have been trying for a couple days now, varying ways of constraining the collection view and it has always yielded the same results. I have searched SO for an answer that may help me resolve this issue but have not been able to come up with something that worked.
I am generally constraining the top of the collection view to the superview (330), the leading and trailing edges to the edges of the superview (0), and then height/width constraints on the UICollectionView to it's dimensions (320, 80).
I tried making a new UIView in IB, constraining it to the outer edges of the superview, and then constraining the collectionView to this new UIView, but it still did the same thing. I'm at a loss here and really appreciate any help you all can give me.
So I figured this out finally. It was completely unrelated to constraints and was in fact a line of code that displaced the entire UICollectionView in iOS 8.
In the -didSelectItemAtIndexPath method I made a call to update the selected indexPath of the collectionView using this line:
[collectionView reloadItemsAtIndexPaths:indexPaths];
I was also pre-selecting an cell at indexPath 0 in my -viewDidLoad so that the first item of the collection would be highlighted, which caused the -didSelectItemAtIndexPath to be called right away.
For some reason, the reloadItemsAtIndexPaths line would change the view of my entire collectionView to the top of the screen.
I replaced this line with:
[collectionView reloadData];
and now everything works fine as intended.

Why UITableView on the device smaller than in IB

When I create my UI in IB I can't do it right because UITableView height not equals its height on the device.
I don't understand why?
Example:
http://img35.imageshack.us/i/20110401111017.jpg
This is happening because the view is being resized when it is pushed on the navigation stack. You can fix this several ways. The best thing to do it setup your nib so it matches the displayed size. You can also remove te autoresize flags from the UITableView. Finally you could programmatically resize the view inside of the view controller's viewWillShow:(BOOL)animated.

Resources