How can we add image in UITableView section header using swift?












10














In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
My code for willDisplayHeaderView is



func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
header.textLabel.textColor = UIColor.whiteColor()
header.alpha = 1
}









share|improve this question



























    10














    In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
    My code for willDisplayHeaderView is



    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
    header.textLabel.textColor = UIColor.whiteColor()
    header.alpha = 1
    }









    share|improve this question

























      10












      10








      10


      5





      In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
      My code for willDisplayHeaderView is



      func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
      let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
      header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
      header.textLabel.textColor = UIColor.whiteColor()
      header.alpha = 1
      }









      share|improve this question













      In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
      My code for willDisplayHeaderView is



      func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
      let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
      header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
      header.textLabel.textColor = UIColor.whiteColor()
      header.alpha = 1
      }






      ios uitableview swift






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 19 '15 at 7:00









      Saqib Omer

      3,40443956




      3,40443956
























          6 Answers
          6






          active

          oldest

          votes


















          8














          You could design your own header, like you would design a custom cell in a table view.
          Or you could just add an image like so:



          func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

          var myCustomView: UIImageView
          var myImage: UIImage = UIImage(named: "myImageResource")
          myCustomView.image = myImage

          let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
          header.addSubview(myCustomView)
          return header
          }


          Then you can simply add your section title UILabel as another subview.






          share|improve this answer

















          • 5




            let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
            – Mikael
            Feb 23 '16 at 2:11










          • I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
            – Parth Barot
            May 23 at 6:42










          • Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
            – shripad20
            Aug 1 at 14:00





















          8














          Simply Add the following code in your viewDidLoad() method.



              var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
          var headerImageView = UIImageView(frame: frame)
          var image: UIImage = UIImage(named: "yourImage")!
          headerImageView.image = image
          youTableView.tableHeaderView = headerImageView





          share|improve this answer































            5














            Here it is how we can add custom header in UITableView.



            func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
            let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
            let image = UIImage(named: "Games.png");
            imageViewGame.image = image;
            header.contentView.addSubview(imageViewGame)
            }





            share|improve this answer































              4














              For Swift 3



                override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
              let header = view as! UITableViewHeaderFooterView
              header.textLabel?.textColor = UIColor.white
              let headerImage = UIImage(named: "headerBanner.jpg")
              let headerImageView = UIImageView(image: headerImage)
              header.backgroundView = headerImageView
              }





              share|improve this answer





























                3














                I use this method below in my proj.



                override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
                {
                let uilbl = UILabel()
                //uilbl.numberOfLines = 0
                //uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
                //uilbl.text = "blablabla"
                uilbl.sizeToFit()
                uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)

                return uilbl
                }





                share|improve this answer































                  2














                  This will work with swift 4



                  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                  let headerImage: UIImage = UIImage(named: "image")!
                  let headerView = UIImageView(image: headerImage)
                  return headerView
                  }





                  share|improve this answer





















                    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%2f28600375%2fhow-can-we-add-image-in-uitableview-section-header-using-swift%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    8














                    You could design your own header, like you would design a custom cell in a table view.
                    Or you could just add an image like so:



                    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

                    var myCustomView: UIImageView
                    var myImage: UIImage = UIImage(named: "myImageResource")
                    myCustomView.image = myImage

                    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                    header.addSubview(myCustomView)
                    return header
                    }


                    Then you can simply add your section title UILabel as another subview.






                    share|improve this answer

















                    • 5




                      let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
                      – Mikael
                      Feb 23 '16 at 2:11










                    • I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
                      – Parth Barot
                      May 23 at 6:42










                    • Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
                      – shripad20
                      Aug 1 at 14:00


















                    8














                    You could design your own header, like you would design a custom cell in a table view.
                    Or you could just add an image like so:



                    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

                    var myCustomView: UIImageView
                    var myImage: UIImage = UIImage(named: "myImageResource")
                    myCustomView.image = myImage

                    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                    header.addSubview(myCustomView)
                    return header
                    }


                    Then you can simply add your section title UILabel as another subview.






                    share|improve this answer

















                    • 5




                      let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
                      – Mikael
                      Feb 23 '16 at 2:11










                    • I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
                      – Parth Barot
                      May 23 at 6:42










                    • Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
                      – shripad20
                      Aug 1 at 14:00
















                    8












                    8








                    8






                    You could design your own header, like you would design a custom cell in a table view.
                    Or you could just add an image like so:



                    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

                    var myCustomView: UIImageView
                    var myImage: UIImage = UIImage(named: "myImageResource")
                    myCustomView.image = myImage

                    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                    header.addSubview(myCustomView)
                    return header
                    }


                    Then you can simply add your section title UILabel as another subview.






                    share|improve this answer












                    You could design your own header, like you would design a custom cell in a table view.
                    Or you could just add an image like so:



                    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

                    var myCustomView: UIImageView
                    var myImage: UIImage = UIImage(named: "myImageResource")
                    myCustomView.image = myImage

                    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                    header.addSubview(myCustomView)
                    return header
                    }


                    Then you can simply add your section title UILabel as another subview.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 19 '15 at 7:47









                    Anfaje

                    215212




                    215212








                    • 5




                      let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
                      – Mikael
                      Feb 23 '16 at 2:11










                    • I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
                      – Parth Barot
                      May 23 at 6:42










                    • Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
                      – shripad20
                      Aug 1 at 14:00
















                    • 5




                      let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
                      – Mikael
                      Feb 23 '16 at 2:11










                    • I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
                      – Parth Barot
                      May 23 at 6:42










                    • Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
                      – shripad20
                      Aug 1 at 14:00










                    5




                    5




                    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
                    – Mikael
                    Feb 23 '16 at 2:11




                    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
                    – Mikael
                    Feb 23 '16 at 2:11












                    I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
                    – Parth Barot
                    May 23 at 6:42




                    I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
                    – Parth Barot
                    May 23 at 6:42












                    Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
                    – shripad20
                    Aug 1 at 14:00






                    Just initialize myCustomView as var myCustomView: UIImageView = UIimageView(). An error will disappear
                    – shripad20
                    Aug 1 at 14:00















                    8














                    Simply Add the following code in your viewDidLoad() method.



                        var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
                    var headerImageView = UIImageView(frame: frame)
                    var image: UIImage = UIImage(named: "yourImage")!
                    headerImageView.image = image
                    youTableView.tableHeaderView = headerImageView





                    share|improve this answer




























                      8














                      Simply Add the following code in your viewDidLoad() method.



                          var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
                      var headerImageView = UIImageView(frame: frame)
                      var image: UIImage = UIImage(named: "yourImage")!
                      headerImageView.image = image
                      youTableView.tableHeaderView = headerImageView





                      share|improve this answer


























                        8












                        8








                        8






                        Simply Add the following code in your viewDidLoad() method.



                            var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
                        var headerImageView = UIImageView(frame: frame)
                        var image: UIImage = UIImage(named: "yourImage")!
                        headerImageView.image = image
                        youTableView.tableHeaderView = headerImageView





                        share|improve this answer














                        Simply Add the following code in your viewDidLoad() method.



                            var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
                        var headerImageView = UIImageView(frame: frame)
                        var image: UIImage = UIImage(named: "yourImage")!
                        headerImageView.image = image
                        youTableView.tableHeaderView = headerImageView






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jun 18 '15 at 3:15









                        Ram

                        2,57993151




                        2,57993151










                        answered Jun 11 '15 at 9:48









                        Muhammad Jabbar

                        393515




                        393515























                            5














                            Here it is how we can add custom header in UITableView.



                            func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                            let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                            let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
                            let image = UIImage(named: "Games.png");
                            imageViewGame.image = image;
                            header.contentView.addSubview(imageViewGame)
                            }





                            share|improve this answer




























                              5














                              Here it is how we can add custom header in UITableView.



                              func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                              let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                              let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
                              let image = UIImage(named: "Games.png");
                              imageViewGame.image = image;
                              header.contentView.addSubview(imageViewGame)
                              }





                              share|improve this answer


























                                5












                                5








                                5






                                Here it is how we can add custom header in UITableView.



                                func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                                let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                                let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
                                let image = UIImage(named: "Games.png");
                                imageViewGame.image = image;
                                header.contentView.addSubview(imageViewGame)
                                }





                                share|improve this answer














                                Here it is how we can add custom header in UITableView.



                                func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                                let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
                                let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
                                let image = UIImage(named: "Games.png");
                                imageViewGame.image = image;
                                header.contentView.addSubview(imageViewGame)
                                }






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Apr 22 '17 at 14:22

























                                answered Feb 19 '15 at 8:01









                                Saqib Omer

                                3,40443956




                                3,40443956























                                    4














                                    For Swift 3



                                      override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                                    let header = view as! UITableViewHeaderFooterView
                                    header.textLabel?.textColor = UIColor.white
                                    let headerImage = UIImage(named: "headerBanner.jpg")
                                    let headerImageView = UIImageView(image: headerImage)
                                    header.backgroundView = headerImageView
                                    }





                                    share|improve this answer


























                                      4














                                      For Swift 3



                                        override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                                      let header = view as! UITableViewHeaderFooterView
                                      header.textLabel?.textColor = UIColor.white
                                      let headerImage = UIImage(named: "headerBanner.jpg")
                                      let headerImageView = UIImageView(image: headerImage)
                                      header.backgroundView = headerImageView
                                      }





                                      share|improve this answer
























                                        4












                                        4








                                        4






                                        For Swift 3



                                          override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                                        let header = view as! UITableViewHeaderFooterView
                                        header.textLabel?.textColor = UIColor.white
                                        let headerImage = UIImage(named: "headerBanner.jpg")
                                        let headerImageView = UIImageView(image: headerImage)
                                        header.backgroundView = headerImageView
                                        }





                                        share|improve this answer












                                        For Swift 3



                                          override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                                        let header = view as! UITableViewHeaderFooterView
                                        header.textLabel?.textColor = UIColor.white
                                        let headerImage = UIImage(named: "headerBanner.jpg")
                                        let headerImageView = UIImageView(image: headerImage)
                                        header.backgroundView = headerImageView
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Mar 16 '17 at 9:38









                                        Arvin Sanmuga Rajah

                                        363310




                                        363310























                                            3














                                            I use this method below in my proj.



                                            override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
                                            {
                                            let uilbl = UILabel()
                                            //uilbl.numberOfLines = 0
                                            //uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
                                            //uilbl.text = "blablabla"
                                            uilbl.sizeToFit()
                                            uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)

                                            return uilbl
                                            }





                                            share|improve this answer




























                                              3














                                              I use this method below in my proj.



                                              override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
                                              {
                                              let uilbl = UILabel()
                                              //uilbl.numberOfLines = 0
                                              //uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
                                              //uilbl.text = "blablabla"
                                              uilbl.sizeToFit()
                                              uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)

                                              return uilbl
                                              }





                                              share|improve this answer


























                                                3












                                                3








                                                3






                                                I use this method below in my proj.



                                                override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
                                                {
                                                let uilbl = UILabel()
                                                //uilbl.numberOfLines = 0
                                                //uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
                                                //uilbl.text = "blablabla"
                                                uilbl.sizeToFit()
                                                uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)

                                                return uilbl
                                                }





                                                share|improve this answer














                                                I use this method below in my proj.



                                                override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
                                                {
                                                let uilbl = UILabel()
                                                //uilbl.numberOfLines = 0
                                                //uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
                                                //uilbl.text = "blablabla"
                                                uilbl.sizeToFit()
                                                uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)

                                                return uilbl
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Oct 31 '15 at 9:44









                                                Hitesh

                                                2,11262541




                                                2,11262541










                                                answered Oct 31 '15 at 8:51









                                                Nik

                                                1017




                                                1017























                                                    2














                                                    This will work with swift 4



                                                    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                                                    let headerImage: UIImage = UIImage(named: "image")!
                                                    let headerView = UIImageView(image: headerImage)
                                                    return headerView
                                                    }





                                                    share|improve this answer


























                                                      2














                                                      This will work with swift 4



                                                      override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                                                      let headerImage: UIImage = UIImage(named: "image")!
                                                      let headerView = UIImageView(image: headerImage)
                                                      return headerView
                                                      }





                                                      share|improve this answer
























                                                        2












                                                        2








                                                        2






                                                        This will work with swift 4



                                                        override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                                                        let headerImage: UIImage = UIImage(named: "image")!
                                                        let headerView = UIImageView(image: headerImage)
                                                        return headerView
                                                        }





                                                        share|improve this answer












                                                        This will work with swift 4



                                                        override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                                                        let headerImage: UIImage = UIImage(named: "image")!
                                                        let headerView = UIImageView(image: headerImage)
                                                        return headerView
                                                        }






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Mar 9 at 15:13









                                                        kroky

                                                        429311




                                                        429311






























                                                            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.





                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                            Please pay close attention to the following guidance:


                                                            • 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%2f28600375%2fhow-can-we-add-image-in-uitableview-section-header-using-swift%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

                                                            Futebolista

                                                            Lallio

                                                            Jornalista