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

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.

Related

Nested UIScrollviews behaving strangely

I have the following autolayout-driven setup:
Main viewController, with a scrollview inside it. Scrollview pinned to superview edges. This one scrolls up and down.
A few normal, fixed size views at the top of the scrollview
Another scrollview. This one scrolls left and right. The second scrollview contains a couple of tableviews, side by side. The idea is that the user can switch between them. They both contain a handful of cells, all the same width as the screen and 72pts tall.
The problem I'm trying to solve is that the tableview contents are not the same size. The left one has say, 6 cells, and the right one has 3.
My first approach was to dynamically change the second scrollview height to match which ever tableview was currently visible. What ended up happening was that switching between the two tableviews (by doing setContentOffset:animated:) went extremely wrong if animated was set to true - it would adjust the content offset so everything was offscreen. In fact it would set the content offset to and then as I switched, about a dozen times, then it'd reset. It was weird, I gave up.
Now I'm trying to just adjust the content inset of the main scrollview to offset the gap in the content of the current tableview, and it's also being weird. When I set the bottom content inset in viewDidLoad, it works fine. When I set it at the time the tableview becomes current, it does nothing.
What gives? What scenarios would lead to these view interactions not behaving properly?
Use different tableViewController for each table.
Embed them in pageViewController.
Add those few normal, fixed size views at the top of the pageViewControllers view.
Conform scrollViewDelegate in pageViewController.
Pass scrollViewDidScroll from tableViews to pageViewController.
Set tableViews inset to match those fixed size views at top.
Change height according to the scroll.
This is the way you can achieve the functionality you want.
I hope it helps.

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

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.

iOS Constraints of image view inside table view cell gets misplaced on scroll

The constraints for the image view inside Table view cell gets distorted on scroll.
The constraints are fine on initial load and view. Once the table view cell goes out of the view and we scroll back to get it, the image view gets displaced.
This is the screenshot of single TableViewCell. The globe icon is misplaced from its original spot (left of name and designation) and overlaps on the text below.
Screenshot of prototype cell from storyboard
The black lines show the constraints added on the storyboard.
Using XCode 8.0 with Swift 3.
I am doing something wrong with the constraints ?
It seems that you are missing a height constraint on the image view. Should be able to fix problem if you add that.
Also, maybe related as well, can you set your image view's content mode to aspect fit? Sometime like this:
ObjC
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
Swift
self.imageView.contentMode = .scaleAspectFit

Adjusting the UiTableview hight in IB so the the table its able to scroll all the rows

I have a UITableView that is cutting off the last row and a half. I assume this is due to the face that I am using a navigation controller. What I can't figure out is how to adjust the height of the TableView so that it works on both iPhone 4 and 5's. If I adjust the height in IB to fit the iPhone 4, I have a large gap on the bottom of the iPhone 5. I would think there is some automatic way of doing this, but nothing I am trying seems to be working.
Auto Layout - Official Guide...
Also, if you're using a UITableViewController on the storyboard, this will solve your problem (with or without navigation controller, which is unrelated). If you're using a UITableView on a regular UIViewController, however, the best answer is auto-layout.

Is it possible set width of UITableView or cells on iPad

Since it's not possible to "hook onto" this and get an answer, and none of the answers do it, I thought I'd ask if anyone ever succeeded in doing it and if so, how.
I've created a New File, selected subclass UITableViewController with XIB, and everything works fine. I can set the height of everything (with delegate methods), but not the frame of anything.
Right now the table is as wide as the iPad and it looks kinda corny with the short titles in each cell - and there is nothing in IB that changes the stretching or layout.
I'd be happy with any solution that causes the cells to shrink horizontally - like margins. I tried scrollview insets in IB, but they only add to the size of the scrollview so it gets bigger than the screen.
I think that you want to subclass UViewController and add a UITableView to it, and make it the size you want in IB.

Resources