TableView into ScrollView - iOS - ios

I would like to insert a UITableView into a UIScrollView.
To do this I added a ContainerView inside the UIScrollView and a UITableView linked to this ContainerView.
The result obtained in the storyboard is this :
http://i.stack.imgur.com/12hZz.jpg (Storyboard)
http://i.stack.imgur.com/AGY4L.jpg (Storyboard)
When I launch the emulator the TableView is loaded correctly, but not with the result expected
http://i.stack.imgur.com/ebij9.gif (Animated GIF)
http://i.stack.imgur.com/bEO8B.jpg (Wrong result)
But instead what I wanted to to obtain is this:
http://i.stack.imgur.com/R7Zhb.jpg
Is it possible?
Could you tell me if the way to proceed is correct?
Thanks!!
I have not enough reputation to post images and more than two links.
Sorry for my poor english.

So what you have is close, but you need to set the size of the container view to the content size of the table view once it has loaded its data. Your other views around the content view then need to respond to the change in size so you don't get overlaps.
If you know the height of each row in the table and how many there are then it's easiest to calculate the hight required and set that as the size of the content view. If you don't, or the height can change based on user choices or something like that, then you will need to do something more complicated which observes the content size of the table view and updates things when it changes.

Related

What's the best approach to display a variable number of images within a TableViewCell in a xib file?

I am building an app about meetings where I want to show a preview of the VIP participants in each meeting cell.
These participants are not clickable nor scrollable, the only purpose it's to quickly see them at a glance.
The problem is that the view is very dynamic:
VIP's appear with image or initials
The rest of the attendees is just a number
if >5 VIP's, the circles start going together overlapping (spacing goes smaller)
if 9 VIPs, big wrapping circle “All VIPs in Attendance"
This is how it will look:
What should I do?
CollectionView (seems over-kill as I am not interested in any kind of interaction with the images)?
StackView?
Images (and change constrains programmatically)?
NOTE:
These is just one kind of Table View Cell, but we have a lot more variations, so we are building the custom cells in xib files. Xcode doesn't allow me to add a Collection View Cell in to the Collection View within the xib.
Interesting task - I'm sure there are numerous approaches, but here is one using UIStackView plus some on-the-fly calculations.
The idea is to define a maximum gap between views; a maximum width for all views; calculate the actual gap needed, and then let UIStackview handle the actual positioning.
Certainly not every feature you need, but should get you going in the right direction.
You can see/download the source for this here: https://github.com/DonMag/ScratchPad
Look at the Swift3/SpreadingViews sub-project for this example.
The problem with scrollable views (UIScrollView based views as UICollectionView), is that you will have to deal with the scroll, or pre-compute the width of your content, which is not always easy. For this reason, if you don't want to have scrollable content, I'd not use a UICollectionView, neither any UIScrollView based view.
Then you have the option to go with an UIStackView. Stack views are great to "append" multiple views and create some kind of "pile" of views in a very easy way. However, if you don't control how many items you need, you will overpass the boundaries of your container view.
Therefore, this is what I'd do:
"Fixed container view width" case: If your container view (your cell) has a fixed width (that never changes), I'd manually add as many UIImageViews I want to support in the XIB itself, and then hide/unhide them depending on the number of items I want to display.
"Variable container view width" case: If your container view (your cell) has a variable width (that changes depending on the screen size or whatever other factor), then you will have to compute in any case (do the math!) the amount of items you are able to display within the width you have available. Then, you can choose between using an UIStackView or adding your views & constraints manually to your container view.
Does what I say make sense?
Thanks,

Set max height of UITableView

on the iPhone 5s it seems as though the last item of a specific list shown using UITableView goes slightly off screen and unable to select. Is their any simple way to prevent the tableview from displaying contents offscreen besides checking overtime data is reloaded and the current height of the view?
EDIT: This is all done programmatically and all other tableviews display correctly on the 5s I have tried fiddling with constraints but could not see a difference.
It sounds like your issue has to do with constraints. I'm not sure whether you're using a storyboard to build the view or not, but probably what you need to do is constrain the table view to the bottom of its superview.

Xcode embed all direct in scrollview without content view

I'm using Xcode 6.4 and I'm struggling with a large page with a bunch of various labels and text fields etc., that has to be scrollable. I looked at various tutorials some of which are obviously outdated and I had all my elements at first in a view that is inside of a scroll view. But that was a big pain with getting all the elements aligned properly using Auto Layout, in addition to not getting it to scroll either.
Now I see one person recommend using the following directly on the elements, without having a content view at all just a scroll view:
Editor -> Embed In -> Scroll View
I like it for the fact that nothing seems out of proportion, all the elements are shown on the iPhone exactly as I have it on the Storyboard without having to set any constraints, however I'm not able to get it to scroll.
This is my class
class ResNotFoundViewController: UIViewController, UIScrollViewDelegate
and I also try to set the height of the scrollview in the code as so:
#IBOutlet weak var scrollviewoutlet: UIScrollView!
and then this in the viewDidLoad as so:
scrollviewoutlet.contentSize = CGSizeMake(375, 840)
.
But I'm not able to get it to scroll at all, it will stay exactly like it opens up with From: Label being the last visible elements.
What am I doing wrong?
You've located the problem, thanks to your logging. (Don't worry about the double call - how many times and when viewDidLayoutSubviews gets called is uninteresting.)
The problem is that you've set the scroll view itself to be taller than the window. In particular, it is 375x809. But so is its contentSize. Thus there is nothing to scroll. A scroll view can only scroll if its contentSize is bigger (in one or two dimensions) than the scroll view. (Moreover, a scroll view bigger than the screen is usually somewhat pointless, since there will be parts of its content that the user will never see - even scrolled all the way, that content will be offscreen.)
So, fix the size of the scroll view itself and you'll be fine. The best way is to assign it constraints that pin its boundaries to its superview, since the superview will have different sizes on different devices.

UICollectionView not able to scroll to see the entire last row

I have a UICollectionView with size: 768x1024 with a navbar on top. My custom UICollectionViewCells are of size 200x200. The problem is that when I keep adding cells and I reach the bottom row, I can only see part of the 200x200 cell. It won't let me scroll further before it bounces back up. Any ideas on what the problem could be?
EDIT:
I add cells via:
insertItemsAtIndexPaths:
The issue was just adding AutoLayout constraints to the View Controller which housed the UICollectionView. To do this, select the View Controller in Storyboards and click on "Editor" on the top. Then, "Resolve Autolayout Issues" > "Reset to Suggested Constraints..."
Check whether you have changed the minimum spacing attribute of collection view in size inspector of collection view. Setting it back to default values(10) was the solution for me.
The problem has to do with your UICollectionViewLayout, since it is the job of the layout to state how large the actual scrollable content is, and to ask for a refresh of that information when necessary. But unfortunately your question reveals nothing about how you are doing layout or how you "keep adding cells", so no specific answer is possible.
EDIT (after your edit): It is not enough to call insertItemsAtIndexPaths:; you must also add the items to your model (the data source). Otherwise, the layout doesn't know about them and doesn't make the scrollable content bigger (and lots of other bad things happen too).
i think you use the collection view in an unsual way, but if the content area of the scrollview of the collection view extend the frame size of the collection view you have to set the virtual size with the property 'contentSize' :
self.collectionView.contentSize = CGSizeMake(768,900);
I had a similar problem but due a gradient that it was at the bottom of the collection view and the user was not able to see the last rows of the collection view.
The way I've found to solve it is in the Size Inspector, set the Height of the Footer Size to a number that works for issue.
I know that maybe, it's not the best solution. But it works for my problem and it's really easy to use it.
Collection View that doesn't show the last row:
Collection View after setting the Footer Size Height to 40:

iOS: How can I figure out the size of the view in the super view's layout?

When I use self.view.frame.size.height, it gets the height of the child nib, rather than the height of how the view is going to fit into the overall layout. I get the same value using self.view.bounds.size.height and super.view.bounds.size.height.
Any help would be appreciated.
Ask at the right time. Not until the view is ready to appear has iOS figured out what size it will actually be. Move your code that depends upon the view being the right size to viewWillAppear instead of viewDidLoad or an init method.
In Xcode 4 you would select your view controller that contains the UIScrollView and set the 'Simulated Metrics' in the IB Attributes Inspector as follows:
This ensures that your UIScrollView will be set to the correct size to fill the available space at design time. It looks like you are using an older version of Xcode/IB and I can't remember exactly where these settings used to be but they're in there somewhere.
With the layout designed to accommodate the space taken up by the tab bar you should be able to query the size of the UIScrollView at any time and get the correct values.

Resources