UICollectionView does resizing when embedded in a container view - ios

I have a view with two container views with different background colours. I am using auto layout. Both container views size themselves correctly (going by the colours). The first view just has a button in it and the child view sizes itself correctly and aligns the button as expected.
The second view has a UICollectionView as the top level view does not size itself. The collection view (with a different background colour) seems to flow off the side of the whole screen. It is definitely way bigger that the container view that it is embedded in.
The Container view has "Autoresize children" set (tried it both ways). I've tried different modes for the container: top, scale, aspect fit.
The Collection view has "Autoresize subviews" set (tried it both ways). I've tried various different modes: Scale, top, aspect fit. The collection view has a custom layout but the problem happens whether it is specified or not.
Does anyone have any ideas on how I can get my UICollectionView to resize itself to match it's parent? I am using Swift it it makes a difference.
I am running XCode 6.4.

Related

Horizontal scroll view not scrolling

I've just vertical scrolling a few times in the past, but now I'm trying to implement horizontal scrolling but when I run it it doesn't scroll.
What is the problem with it?
P.S. is it possible to design and layout views using a storyboard for scrolling? In this example the blue view is still visible within the screen representation, so its ok to deal with, but suppose I wanted to add another view which is further to the right and thus not visible within the screen representation? Is there anyway of visually designing a scroll view where you can see all the entirity within the storyboard what it will look like?
You don't have a trailing constraint on the scroll view's direct subview, so the scroll view cannot compute the correct contentSize.
Also, it's easier to understand the constraints in the document outline if you give each view a unique label. You have two views labelled “View” so it's difficult to be sure which constraints connect to which “View”.

Multipage UICollectionView with View

I am trying to create a collection view layout similar to the one used by Twitter and Product Hunt. I want to have a horizontal collection view to tab between data while also maintaining vertical scrolling in the tab I am in. I do not want to just change the datasource that is loaded into the UICollectionView. I have tried several different layouts but none of them have been successful.
Right now, I have a UIViewController with a UICollectionView & UIImageView object inside of it. The UIImageView's size is (3/8) of the UIViewController's frame and begins at (0,0). The UICollectionView's frame is equal to the UIViewController's frame but the cv is offset to be below UIImageView & its size is equal to the size of the UIViewController's view. The (1) cell inside the UICollectionView has a UICollectionView inside of it -- this can be configured with the data you want (multiple cells). This layout works fine for a single datasource.
However, when you want to have multiple datasources accessed by scrolling horizontally the layout breaks down. You have to change the flow layout of the outermost UICollectionView to horizontal. When you do this, you get a layout error because the size of the collection view is larger than than the view and you cannot scroll vertically.
If you reference the image I provided, I think you can infer what the desired layout should behave like. You should be able to access the other datasource by horizontal scrolling but also be able to scroll the current collection view full screen and not have it fixed to the bottom of the UIImageView. I tried adding logic to the scrollview delegate methods but that didn't work either. Thanks]2

UICollectionView screen-sized cell rotation?

Imagine a composition like this ->
UIViewController with root view containing these subviews :
- header view pinned to top,
- footer view pinned to bottom,
- scrollview matching the screen bounds with uiimageview as content where the image of the image view is a landscape image and the imageview size is aspect fitting the scrollview, zommscale is 1.
If I rotate this controller, thanks to proper layout(doesnt matter if manual or autolayout) the resulting animation to new landscape screen bounds is flawless.
If I put this controller as a member of UIPageViewController multiple times (effectively creating a gallery) the rotation of any particular page, containing a controller from above is also flawless.
In both cases above, none of the layers dissapear, the resizing animation nicely comforms to layout rules. The animation as a whole makes complete sense.
Now let's create a Collection controller with a horizontal flow layout. Zero insets, zero interitem size, and the cell size matching one screen. We ended up with horizontal gallery right?
So why, when I rotate the device, is the final animation composition so messy?
I slowed down the animation and what I see is views from neighbouring controllers temporarily "leaking in"
the central image view is deformed while animated and eventually thrown away, and finaly it calms down and a new! cell content is shown with proper layout that comforms the landscape device orientation. It's like those views are not sure what they want and behave like drunk.
Why cant the collection view rotate in such a way that only the cell will resize alongside the animation coordinator and firmly keep it's center in the center of screen?
How to force collection view to resize it's current and only visible cell in same manner as if it was a root view?

Scrolling storyboard elements that are off screen?

I am using Xcode 5 and ios 7. I have a view which has size 320x1200 which contains various static elements such as buttons,images etc.
I created View controller,
I added Scroll View of same size to this View Controller
I Added View to Scroll View and changed the size of the view to 320x1200
I selected main/root View Controller and used EDITOR -> Resolve Auto Layout..-> Add Missing Constraints (to enable scroll).
Now I can scroll but I still cannot see the off screen content. Is there any way to resolve this.
Resize the Scroll View to the root view's size (e.g. 320x480 on iPhone 4) and set its content size to 320x1200 instead.

Best time in View lifecycle to determine the on-screen size of a UIScrollView

I have a .xib containing a view which itself containing top and bottom accessory views (navigation, a page control) and a UIScrollView. Its height varies depending on whether I'm running on an iPhone 4 or 5 (3.5" or 4"). At runtime I move the containing view offscreen, populate the scrollview with UIButtons to build a scrollable launchpad, and scroll this on to the screen from the bottom. Tapping a button shows another view.
On a smaller screen I'd like to show only three buttons in the scrollview, on the larger one I show four. So I need to know the height that the scrollview will be when it appears, before it actually appears. I'd hoped to have this information available at some point in the view lifecycle (viewWillAppear etc.) but the only place this seems to be correct is in viewDidAppear. At this point the view is already on screen and creating the buttons then means they appear suddenly, and are not scrolled nicely onscreen.
The top-level view in the .xib is set to Retina 4 FullScreen.
I'm happy to concede that viewWillAppear is the correct place to create the buttons and my navigation controller is perhaps instantiating the view incorrectly, but my iOS-fu isn't strong enough to say for certain.
My solution has been to detect screen size and hardwire the button height. Is there a better (more elegant, future-proof and canonical) way to do this?
Don't worry, you are in the right direction:
viewWillAppear is the correct method in a view's lifecycle, where the frames of that view and its subviews are completely calculated and nothing has been drawn yet. Here you can make corrections to the standard behavior of your view.
When viewDidAppear gets called, you get the same information about positions and sizes of view elements as in viewWillAppear. But at this time, the animations and the view itself already have been drawn.
That is, why you'll want to use viewWillApear.

Resources