ios UICollectionView remove header programmatically












3















I want have a control of the header of a UICollectionView as I need to remove and add it based on user-generated events.



What I have tried so far:



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

if(toRemoveHeader){

return CGSizeZero;

}else{

return CGSizeMake(320, 45);

}
}


And then call [self.collectionView reloadData] whenever the user-event is generated. I would prefer to make this without reloading the data. Any ideas?










share|improve this question

























  • Hi! How you has solved this? =D

    – Adriano Tadao
    Oct 13 '14 at 17:29











  • I dont think i solved it...

    – Petar
    Oct 14 '14 at 7:08
















3















I want have a control of the header of a UICollectionView as I need to remove and add it based on user-generated events.



What I have tried so far:



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

if(toRemoveHeader){

return CGSizeZero;

}else{

return CGSizeMake(320, 45);

}
}


And then call [self.collectionView reloadData] whenever the user-event is generated. I would prefer to make this without reloading the data. Any ideas?










share|improve this question

























  • Hi! How you has solved this? =D

    – Adriano Tadao
    Oct 13 '14 at 17:29











  • I dont think i solved it...

    – Petar
    Oct 14 '14 at 7:08














3












3








3








I want have a control of the header of a UICollectionView as I need to remove and add it based on user-generated events.



What I have tried so far:



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

if(toRemoveHeader){

return CGSizeZero;

}else{

return CGSizeMake(320, 45);

}
}


And then call [self.collectionView reloadData] whenever the user-event is generated. I would prefer to make this without reloading the data. Any ideas?










share|improve this question
















I want have a control of the header of a UICollectionView as I need to remove and add it based on user-generated events.



What I have tried so far:



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

if(toRemoveHeader){

return CGSizeZero;

}else{

return CGSizeMake(320, 45);

}
}


And then call [self.collectionView reloadData] whenever the user-event is generated. I would prefer to make this without reloading the data. Any ideas?







ios uicollectionview uicollectionviewlayout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:48









Cœur

19k9114155




19k9114155










asked Mar 14 '14 at 9:29









PetarPetar

1,48011935




1,48011935













  • Hi! How you has solved this? =D

    – Adriano Tadao
    Oct 13 '14 at 17:29











  • I dont think i solved it...

    – Petar
    Oct 14 '14 at 7:08



















  • Hi! How you has solved this? =D

    – Adriano Tadao
    Oct 13 '14 at 17:29











  • I dont think i solved it...

    – Petar
    Oct 14 '14 at 7:08

















Hi! How you has solved this? =D

– Adriano Tadao
Oct 13 '14 at 17:29





Hi! How you has solved this? =D

– Adriano Tadao
Oct 13 '14 at 17:29













I dont think i solved it...

– Petar
Oct 14 '14 at 7:08





I dont think i solved it...

– Petar
Oct 14 '14 at 7:08












2 Answers
2






active

oldest

votes


















1














Your implementation is fully functional, the problem might be that you don't assign the object implementing that function to the delegate property of your collectionView.



The function collectionView:layout:referenceSizeForHeaderInSection: is implemented by a class confirming to the UICollectionViewDelegateFlowLayout protocol, and the collectionView expects its delegate to implement this method, and not its dataSource.



In one of my implementations I show a footer only if there are no cells in that section, and as long as the delegate property is set correctly it works perfectly.



#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize) collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section
{
NSUInteger count = [self collectionView: collectionView
numberOfItemsInSection: section];
CGFloat footerHeight = (count == 0) ? 60.f : 0.f;
CGFloat footerWidth = collectionView.frame.size.width;
return CGSizeMake(footerWidth, footerHeight);
}





share|improve this answer































    1














    If you are using Swift you can do it this way in your UICollectionViewController subclass:



    var hideHeader: Bool = true //or false to not hide the header

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if hideHeader {
    return CGSizeZero //supplementary view will not be displayed if height/width are 0
    } else {
    return CGSizeMake(30,80) //size of your UICollectionReusableView
    }
    }





    share|improve this answer


























    • or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

      – gammachill
      Feb 25 '16 at 15:59













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22400929%2fios-uicollectionview-remove-header-programmatically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your implementation is fully functional, the problem might be that you don't assign the object implementing that function to the delegate property of your collectionView.



    The function collectionView:layout:referenceSizeForHeaderInSection: is implemented by a class confirming to the UICollectionViewDelegateFlowLayout protocol, and the collectionView expects its delegate to implement this method, and not its dataSource.



    In one of my implementations I show a footer only if there are no cells in that section, and as long as the delegate property is set correctly it works perfectly.



    #pragma mark - UICollectionViewDelegateFlowLayout

    - (CGSize) collectionView:(UICollectionView *)collectionView
    layout:(UICollectionViewLayout *)collectionViewLayout
    referenceSizeForFooterInSection:(NSInteger)section
    {
    NSUInteger count = [self collectionView: collectionView
    numberOfItemsInSection: section];
    CGFloat footerHeight = (count == 0) ? 60.f : 0.f;
    CGFloat footerWidth = collectionView.frame.size.width;
    return CGSizeMake(footerWidth, footerHeight);
    }





    share|improve this answer




























      1














      Your implementation is fully functional, the problem might be that you don't assign the object implementing that function to the delegate property of your collectionView.



      The function collectionView:layout:referenceSizeForHeaderInSection: is implemented by a class confirming to the UICollectionViewDelegateFlowLayout protocol, and the collectionView expects its delegate to implement this method, and not its dataSource.



      In one of my implementations I show a footer only if there are no cells in that section, and as long as the delegate property is set correctly it works perfectly.



      #pragma mark - UICollectionViewDelegateFlowLayout

      - (CGSize) collectionView:(UICollectionView *)collectionView
      layout:(UICollectionViewLayout *)collectionViewLayout
      referenceSizeForFooterInSection:(NSInteger)section
      {
      NSUInteger count = [self collectionView: collectionView
      numberOfItemsInSection: section];
      CGFloat footerHeight = (count == 0) ? 60.f : 0.f;
      CGFloat footerWidth = collectionView.frame.size.width;
      return CGSizeMake(footerWidth, footerHeight);
      }





      share|improve this answer


























        1












        1








        1







        Your implementation is fully functional, the problem might be that you don't assign the object implementing that function to the delegate property of your collectionView.



        The function collectionView:layout:referenceSizeForHeaderInSection: is implemented by a class confirming to the UICollectionViewDelegateFlowLayout protocol, and the collectionView expects its delegate to implement this method, and not its dataSource.



        In one of my implementations I show a footer only if there are no cells in that section, and as long as the delegate property is set correctly it works perfectly.



        #pragma mark - UICollectionViewDelegateFlowLayout

        - (CGSize) collectionView:(UICollectionView *)collectionView
        layout:(UICollectionViewLayout *)collectionViewLayout
        referenceSizeForFooterInSection:(NSInteger)section
        {
        NSUInteger count = [self collectionView: collectionView
        numberOfItemsInSection: section];
        CGFloat footerHeight = (count == 0) ? 60.f : 0.f;
        CGFloat footerWidth = collectionView.frame.size.width;
        return CGSizeMake(footerWidth, footerHeight);
        }





        share|improve this answer













        Your implementation is fully functional, the problem might be that you don't assign the object implementing that function to the delegate property of your collectionView.



        The function collectionView:layout:referenceSizeForHeaderInSection: is implemented by a class confirming to the UICollectionViewDelegateFlowLayout protocol, and the collectionView expects its delegate to implement this method, and not its dataSource.



        In one of my implementations I show a footer only if there are no cells in that section, and as long as the delegate property is set correctly it works perfectly.



        #pragma mark - UICollectionViewDelegateFlowLayout

        - (CGSize) collectionView:(UICollectionView *)collectionView
        layout:(UICollectionViewLayout *)collectionViewLayout
        referenceSizeForFooterInSection:(NSInteger)section
        {
        NSUInteger count = [self collectionView: collectionView
        numberOfItemsInSection: section];
        CGFloat footerHeight = (count == 0) ? 60.f : 0.f;
        CGFloat footerWidth = collectionView.frame.size.width;
        return CGSizeMake(footerWidth, footerHeight);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 9 '16 at 15:47









        AerowsAerows

        538418




        538418

























            1














            If you are using Swift you can do it this way in your UICollectionViewController subclass:



            var hideHeader: Bool = true //or false to not hide the header

            func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            if hideHeader {
            return CGSizeZero //supplementary view will not be displayed if height/width are 0
            } else {
            return CGSizeMake(30,80) //size of your UICollectionReusableView
            }
            }





            share|improve this answer


























            • or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

              – gammachill
              Feb 25 '16 at 15:59


















            1














            If you are using Swift you can do it this way in your UICollectionViewController subclass:



            var hideHeader: Bool = true //or false to not hide the header

            func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            if hideHeader {
            return CGSizeZero //supplementary view will not be displayed if height/width are 0
            } else {
            return CGSizeMake(30,80) //size of your UICollectionReusableView
            }
            }





            share|improve this answer


























            • or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

              – gammachill
              Feb 25 '16 at 15:59
















            1












            1








            1







            If you are using Swift you can do it this way in your UICollectionViewController subclass:



            var hideHeader: Bool = true //or false to not hide the header

            func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            if hideHeader {
            return CGSizeZero //supplementary view will not be displayed if height/width are 0
            } else {
            return CGSizeMake(30,80) //size of your UICollectionReusableView
            }
            }





            share|improve this answer















            If you are using Swift you can do it this way in your UICollectionViewController subclass:



            var hideHeader: Bool = true //or false to not hide the header

            func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            if hideHeader {
            return CGSizeZero //supplementary view will not be displayed if height/width are 0
            } else {
            return CGSizeMake(30,80) //size of your UICollectionReusableView
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 1 '16 at 9:26









            Natan R.

            4,42312545




            4,42312545










            answered Feb 25 '16 at 15:58









            gammachillgammachill

            878813




            878813













            • or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

              – gammachill
              Feb 25 '16 at 15:59





















            • or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

              – gammachill
              Feb 25 '16 at 15:59



















            or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

            – gammachill
            Feb 25 '16 at 15:59







            or if you want to get the existing size of the header view as specified in the storyboard, replace the CGSizeMake(30,80) line with this: let headerView = self.view.subviews[0].subviews[0] as! UICollectionReusableView let existingSize = headerView.frame.size return existingSize

            – gammachill
            Feb 25 '16 at 15:59




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22400929%2fios-uicollectionview-remove-header-programmatically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas