UICollectionView doesn't bounce when datasource has only 1 item - iphone

I have a UICollectionView with a custom UICollectionViewFlowLayout. It scrolls horizontally and only shows one item at a time (full height). I have pagingEnabled = YES so that it sticks to pages.
The UICollectionView shows photos within a particular day. I initially had buttons to change the day +/- 1 day, but I thought it would be neat if the user could pull on the collection view past the first or last image (past a predetermined threshold) to change the day. I implemented this using the UIScrollView delegate methods and it works great...... if there are 2 or more items in the collection view.
This is hard to describe, but if there is only 1 item, it doesn't allow me to pull the item past it's bounds. I.E. there is no bounce to it.
I'd like to have this behavior, but I need to overcome this problem first. I'm thinking that if I removed pagingEnabled = YES and used targetContentOffsetForProposedContentOffset:proposedContentOffset:withScrollingVelocity, that I may be able to get it to work but I really enjoy the snappy behavior that paging enables. (They are mutually exclusive).
Any ideas on this?

UICollectionView has the properties alwaysBounceHorizontal and alwaysBounceVertical. Setting the horizontal to YES did the trick.

Thanks for #VaporwareWolf ,the answer is correct. I want to add a supplement that:
DO NOT forget setting the bounces property to YES at the same time(through storyboard or your code).

Related

Better to use UIScrollView or UITableView for horizontal buttons?

I have a page enabled scrollview on an iPad. On the first page, I have a child scrollview that scrolls horizontally through image buttons. The buttons scroll the outer scroll view to the correct page. Its basically like a table of contents that jumps to the correct page.
My end goal is to be able to categorize the buttons seen in the child scroll view. So there would be a segmented control that changes what buttons you can see. So maybe one category would be ALL, and another category would be A-M, and another would be N-Z for example.
My question is, should I use a uiscrollview or a uitableview?
Right now I use a scrollview and it is really easy to get the buttons in. I could implement the different categories kind of gimmicky by having all of the buttons in the scrollview and then just showing or hiding the buttons accordingly. I feel that it'd be bad memory usage though.
For a uiscrollview i was looking at using EasyTableView, butI'm not 100% sure if this is compatible with what i want to do or if it'd even be better.
Any ideas for what the best way to implement this is? Specifically, I'm not sure of the best way to change the buttons when I change categories.
Thanks!
Use a tableview when you are dealing with data that is best expressed as sections and rows.
I think for your situation I'd have a UIView subclass that can display the images you need for a given category. Stick that on the outer scrollview as needed. You can keep memory low by only keeping the currently visible view and the ones on either side on the scrollview. When you scroll to a new location you can recreate the view needed for that page, and the ones surrounding it. Then you release the ones that are far away and let the system reclaim their memory if needed.

Get selected item in UIScrollView?

I'm trying to make an app where the user has a "hand" of cards, and they can select one (it does more than that, as just that would be entertaining for about 3 seconds). Currently, the "hand" is represented by a horizontal, paging UIScrollView. Each card is a UIImageView. Things scroll nicely, but where I'm stuck at is trying to bubble up the selected card to the controller. What's the best way to do this?
You can directly use the contentOffset to judge the current page.
int cardIndex = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
You might've to round it though. You should be able to get the card index using the snippet above. However it doesn't hurt to maintain an instance variable which you would update in the delegate method scrollViewDidScroll:.
Or you can look at UIPageControl too which kinda does something similar but a bit more overhead for your case.
I would go for the extra instance variable or property.

How to achieve paginated UITableView like in this example?

I am attaching an image. Some of you might have tried this iphone application before. It is a screen shot from Awesome Note. How do they achieve the 5 rows table view, with a swipe to next page for more rows?
Basically the UITableView (at least I think it's a TableView?) is confined to the bottom half of the UIView.
Any swipe to the left or right jumps to the next page and you see the next 5 rows (if any.) We can see from the screenshot that there are at least 11 rows, since it's 3 pages in depth.
Is this achieved using a TableViewController inside a UIScrollView or something? I've setup my own custom TableViewController, along with a UITableView. Inside interface builder when I check Attributes section for my UITableView I see an checkbox for Paging Enabled, which I've ticked.
I've also disabled Vertical Scrollers but this seems to have no effect? I'm obviously missing something, seeing as I haven't even seen where I can define how many rows / sections I'd like to render on one page before another page is created.
Any help would be appreciated.
Thanks!
Thats not a UITableView, thats a UIScrollView with custom buttons.
The Paging option is because UITableView is a sub-class of UIScrollView.
If there are only 5 items, it's better to use 5 overlapping UIButtons

Displaying one graph per day, home screen style

I'm working on an app that can display a graph with some data, one every day. I'll call this "dayGraph".
I'd like to build an interface similar to the iPhone home screen, aka pageControl, with one dayGraph per page. (but i don't need the small dots!)
I tried with the standard pageControl example from Apple, and it's working, but only with a small number of pages. I need to display even 100 possible dayGraph, if the user wants, but the pageControl is going crazy when the page number exceed 50.
I know that the photo app is doing a similar thing, and it can work with many photos.
My question is: How can i do that? Is there any "Apple way" of doing it, or i should begin to make my own method for swapping the dayGraphs?
Thank you very much!
If your goal is to enable swipe-based paging, the view you want to use is a UIScrollView. Set its pagingEnabled property to YES. Then add each graph as a subview of your UIScrollView, setting the frame of each graph to position each one on its own page extending beyond the bounds of your UIScrollView to the right. Finally, update the contentSize property of your UIScrollView to encompass all of your pages.
Now the user can swipe left and right to flip between the pages of your scroll view.
Note: If you add 50+ subviews to your scrollview, it's likely that you'll run into memory problems. To avoid this, you'll probably want just keep subviews loaded for the current page, plus the adjacent pages to the right and left. Set a delegate on your UIScrollView to an object that implements scrollViewDidEndDecelerating:. In that method, remove any subviews that aren't on the current or adjacent pages, and make sure each adjacent page has its subview loaded. That way you'll never have more than 3 pages of content in memory at one time.

UITableView doesn't scroll when number of items is less than scrollable limit

I've a UITableView and I populate data from a service. When the number of rows is higher than what could be displayed on the screen, I'm able to scroll the table. But when it's less (say 1 or 2), I'm unable to scroll (in the sense, the bouce effect doesn't work making the UI appear odd).
I think this should be fixed by some IB checkbox, but not sure how to do it. Anyone faced this issue before?
The solution that worked for me was to set bounces to YES programmatically in viewDidLoad.
I also have bounces set to YES using the checkbox in IB. It does appear to be taking the IB value because bouncing will work after I've pushed another view controller onto the navigation controller stack and then popped it back off. However, bouncing won't work when the table view first appears unless I set bounces to YES in viewDidLoad.
Click on the table view and look at the Attributes Inspector. There's a checkbox for Bounce Scroll. (I just tested this in a project that had a 2-line table; disabling it prevented scrolling, enabling it allows the short table to bounce)
I think the programatic equivalent is the bounces property.
Do you have this table view as a cell inside another table view?