How do I programmatically put a UICollectionView in a UIScrollView?

Discussion in 'iOS Programming' started by Reach9, Jan 5, 2016.

  1. Reach9 macrumors 68020

    Reach9

    Joined:
    Aug 17, 2010
    Location:
    In America
    #1
    Hi all, I'm self learning iOS development and pretty new to it. I'm doing it in Objective C.

    I have made a UICollectionViewController of images and I'm trying to show that on my MainViewController which has a UIScrollView because I want the UITextField to move up on tap of a button which reveals the UICollectionView from below the bottom.
    Should I put another UIView there?

    How do I get the images from my UICollectionViewController to show in the UIScrollView section in my MainViewController?
    In my current UICollectionViewController the images (cells) are taking up the entire view.

    I'm doing this programmatically to really understand the language.

    Any help would be appreciated! Thank you.
     
  2. JWalker1995 macrumors regular

    Joined:
    Mar 5, 2011
    #2
    Wow, first thing I would definitely not recommend doing it all programmatically, that's really not needed and will never be used if you continue coding. Also, it's not really recommend to put a UICollectionView inside a scroll vie as they both support vertical scrolling, so can make the user experience terrible as you try to scroll one but end up scrolling the other and it turns into a nightmare.

    Just checking, do you want the UICollectionViewController to appear full screen, on just the bottom bit below your textured and button?

    If yes, on the same screen

    To answer the question, two options easy and hard
    Easy - don't use a UICollectionViewController, just use a UICollectionView inside your MainViewController UIScrollView but will require some code changes and the UICollectionView won't have its own class, it'll just be within the MainViewController class. You can then move the UICollectionView in and out of view as you please.

    Hard(ish)
    Look up ChildViewControllers, you can convert the UICollectionViewController in a view programmatically and add it as a subview to the MainViewController. But it's a little tricky and can take a while to get right. As your pretty new it's not something I'd recommend. Get it working with the easy option and then try this.

    If no, it's should be full screen, just do [self.navigationController presentViewController:animated:] And pass in your UICollectionViewController name and a bool for animated.

    If you have your code on a public git feel free to send me a link and I'll take a look
     
  3. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #3
    If you look up some of the examples of doing a button or other view, you'll see the basics.
    Code:
    var DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100))
    DynamicView.backgroundColor=UIColor.greenColor()
    DynamicView.layer.cornerRadius=25
    DynamicView.layer.borderWidth=2
    self.view.addSubview(DynamicView)
    Setup a var.
    Set the parameters (settings)
    Add the item to a view.

    It's good to know how things work under the hood. As I understand it, everything can be done programmatically, but I don't know how many actually do this.
     
  4. Reach9 thread starter macrumors 68020

    Reach9

    Joined:
    Aug 17, 2010
    Location:
    In America
    #4
    Hey thanks for the reply! Yeah you're right, it's not necessary but I like to dive in deep to learn the language and figure it out programmatically.

    You're right, I want to put the collectionview on the same screen just below the UITextField and UIButton I made.

    I'd like to try both the Easy & Hard ways you stated. However I'm having difficulty getting my UICollectionViewController into just a UICollectionView to show up in my MainViewController UIScrollView, how do I do that? (Noob question I'm sure)
    I've already made a UICollectionViewController with everything so I'm wondering if it would be easier to just do it the hard way?

    I'm just learning about Git and version control so I'll definitely post a link to the github once I figured this out, thank you!
     
  5. JWalker1995 macrumors regular

    Joined:
    Mar 5, 2011
    #5
    On your mainViewController make a UICollectionView, inside view did load, do alloc init, give it a position through constraints, and set .delegate and .datasource = self. At the top in the class declaration next to the @interface, you then need to add the UICollectionViewDelegate and UICollectionViewDataSource. Then Just copy the methods over from you UICollectionViewController that relate to the creation of the UICollectionView (mainly numberOfItemsInSection and cellForItenAtIndexPath)
     
  6. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #6
    It's ok to put it in a scroll view as long as you disable scrolling on the collection view. It would make no sense if you didn't.
     
  7. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #7
    First of all realise the difference between a view and a viewController. In general you only want a single viewController per screen. Any more is an advanced topic and probably not necessary for what you want to do.

    Second, UIScrollView is a class a lot of other classes inherit from, UICollectionView being one. This means they all listen to the same things and they easily end up interfering with each other. Having a collectionView inside a scrollView is possible, but again, an advanced topic and I don't think it's necessary for what you want.

    Third, and this is from experience only - Avoid using the UICollectionViewController and UITableViewController. Their limitations outweigh their benefits.

    In your root UIViewController there is a view. To this view you can add the button somewhere in the visible space and the label you are looking to move somewhere off screen. You can also add the UICollectionView but leave it off screen.

    Then when you press the button you can animate the frame property of both the label and the collectionView. So instead of scrolling the content along a scrollview you programatically move the content in the view. This is a good example of things easier done in code than IB.
     

Share This Page