How to set UITableView height accroding to its content height?












0















I have a question about UITableView.

I want to let the tableView height according to my cells content height.

So, I use the following code successfully.



DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}


But when I have much data to show, my contents will out of screen.

And the tableView also out of screen.

Have any ideas to set it's constrain and don't make it out of screen.

I want to set 15 between the tableView bottom layout and superView bottom layout.

I use SnapKit to set autolayout.



//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.

tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}









share|improve this question

























  • I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?

    – dahiya_boy
    Nov 23 '18 at 7:37











  • Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.

    – JimmyLee
    Nov 23 '18 at 7:40











  • tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50

    – JimmyLee
    Nov 23 '18 at 7:41













  • Can you show me your full code so that I can clearly know what is your requirement?

    – Ashvini
    Nov 23 '18 at 9:48











  • I don't undestand what you want here?

    – Ashvini
    Nov 23 '18 at 9:49
















0















I have a question about UITableView.

I want to let the tableView height according to my cells content height.

So, I use the following code successfully.



DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}


But when I have much data to show, my contents will out of screen.

And the tableView also out of screen.

Have any ideas to set it's constrain and don't make it out of screen.

I want to set 15 between the tableView bottom layout and superView bottom layout.

I use SnapKit to set autolayout.



//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.

tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}









share|improve this question

























  • I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?

    – dahiya_boy
    Nov 23 '18 at 7:37











  • Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.

    – JimmyLee
    Nov 23 '18 at 7:40











  • tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50

    – JimmyLee
    Nov 23 '18 at 7:41













  • Can you show me your full code so that I can clearly know what is your requirement?

    – Ashvini
    Nov 23 '18 at 9:48











  • I don't undestand what you want here?

    – Ashvini
    Nov 23 '18 at 9:49














0












0








0


2






I have a question about UITableView.

I want to let the tableView height according to my cells content height.

So, I use the following code successfully.



DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}


But when I have much data to show, my contents will out of screen.

And the tableView also out of screen.

Have any ideas to set it's constrain and don't make it out of screen.

I want to set 15 between the tableView bottom layout and superView bottom layout.

I use SnapKit to set autolayout.



//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.

tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}









share|improve this question
















I have a question about UITableView.

I want to let the tableView height according to my cells content height.

So, I use the following code successfully.



DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}


But when I have much data to show, my contents will out of screen.

And the tableView also out of screen.

Have any ideas to set it's constrain and don't make it out of screen.

I want to set 15 between the tableView bottom layout and superView bottom layout.

I use SnapKit to set autolayout.



//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.

tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}






ios swift uitableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:42









Damon

520318




520318










asked Nov 23 '18 at 7:33









JimmyLeeJimmyLee

12411




12411













  • I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?

    – dahiya_boy
    Nov 23 '18 at 7:37











  • Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.

    – JimmyLee
    Nov 23 '18 at 7:40











  • tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50

    – JimmyLee
    Nov 23 '18 at 7:41













  • Can you show me your full code so that I can clearly know what is your requirement?

    – Ashvini
    Nov 23 '18 at 9:48











  • I don't undestand what you want here?

    – Ashvini
    Nov 23 '18 at 9:49



















  • I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?

    – dahiya_boy
    Nov 23 '18 at 7:37











  • Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.

    – JimmyLee
    Nov 23 '18 at 7:40











  • tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50

    – JimmyLee
    Nov 23 '18 at 7:41













  • Can you show me your full code so that I can clearly know what is your requirement?

    – Ashvini
    Nov 23 '18 at 9:48











  • I don't undestand what you want here?

    – Ashvini
    Nov 23 '18 at 9:49

















I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?

– dahiya_boy
Nov 23 '18 at 7:37





I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?

– dahiya_boy
Nov 23 '18 at 7:37













Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.

– JimmyLee
Nov 23 '18 at 7:40





Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.

– JimmyLee
Nov 23 '18 at 7:40













tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50

– JimmyLee
Nov 23 '18 at 7:41







tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50

– JimmyLee
Nov 23 '18 at 7:41















Can you show me your full code so that I can clearly know what is your requirement?

– Ashvini
Nov 23 '18 at 9:48





Can you show me your full code so that I can clearly know what is your requirement?

– Ashvini
Nov 23 '18 at 9:48













I don't undestand what you want here?

– Ashvini
Nov 23 '18 at 9:49





I don't undestand what you want here?

– Ashvini
Nov 23 '18 at 9:49












6 Answers
6






active

oldest

votes


















0














Have you tried



self.tableView.invalidateIntrinsicContentSize()


https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize






share|improve this answer
























  • sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

    – JimmyLee
    Nov 23 '18 at 7:52



















0














Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:



DispatchQueue.main.async {
if let superViewHeight = self.tableView.superView?.bounds.maxY {
let maxHeight = superViewHeight - self.tableView.frame.minY
var frame = self.tableView.frame
frame.size.height = min(self.tableView.contentSize.height, maxHeight)
self.tableView.frame = frame
}
}





share|improve this answer































    0














    Code Work :



    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
    @IBOutlet weak var tblView: UITableView!

    var tblMaxHeight : CGFloat = 50

    override func viewDidLoad() {
    super.viewDidLoad()

    let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height

    tblMaxHeight = self.view.frame.size.height - 40 - navHeight

    }


    override func viewDidLayoutSubviews(){

    tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 24
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

    cell?.textLabel?.text = "Row: #(indexPath.row)"

    return cell!
    }


    }


    Constraints to tableView :



    enter image description here



    Output :



    enter image description here



    enter image description here






    share|improve this answer

































      0














      Put your UITableView inside a UIScrollView and add constraints like this:



      Constraints to Table View



      enter image description here



      Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController



      override func viewWillLayoutSubviews() {
      super.updateViewConstraints()
      self.tableViewHeightConstraint.constant = tableView.contentSize.height
      }





      share|improve this answer































        0














        I have solved a similar problem using the following method. You only need few lines of code.



        override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
        }

        private func setupTableView() {
        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableViewAutomaticDimension
        }


        Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.






        share|improve this answer































          0














          You could create a custom UITableView :



          class AutomaticHeightTableView: UITableView {

          override var contentSize: CGSize {
          didSet {
          self.invalidateIntrinsicContentSize()
          }
          }

          override var intrinsicContentSize: CGSize {
          self.layoutIfNeeded()
          return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
          }

          }


          And then set your UITableView Class to AutomaticHeightTableView.



          This solution is inspired from an answer found on stackoverflow.






          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%2f53442384%2fhow-to-set-uitableview-height-accroding-to-its-content-height%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









            0














            Have you tried



            self.tableView.invalidateIntrinsicContentSize()


            https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize






            share|improve this answer
























            • sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

              – JimmyLee
              Nov 23 '18 at 7:52
















            0














            Have you tried



            self.tableView.invalidateIntrinsicContentSize()


            https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize






            share|improve this answer
























            • sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

              – JimmyLee
              Nov 23 '18 at 7:52














            0












            0








            0







            Have you tried



            self.tableView.invalidateIntrinsicContentSize()


            https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize






            share|improve this answer













            Have you tried



            self.tableView.invalidateIntrinsicContentSize()


            https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 7:44









            DamonDamon

            520318




            520318













            • sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

              – JimmyLee
              Nov 23 '18 at 7:52



















            • sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

              – JimmyLee
              Nov 23 '18 at 7:52

















            sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

            – JimmyLee
            Nov 23 '18 at 7:52





            sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.

            – JimmyLee
            Nov 23 '18 at 7:52













            0














            Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:



            DispatchQueue.main.async {
            if let superViewHeight = self.tableView.superView?.bounds.maxY {
            let maxHeight = superViewHeight - self.tableView.frame.minY
            var frame = self.tableView.frame
            frame.size.height = min(self.tableView.contentSize.height, maxHeight)
            self.tableView.frame = frame
            }
            }





            share|improve this answer




























              0














              Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:



              DispatchQueue.main.async {
              if let superViewHeight = self.tableView.superView?.bounds.maxY {
              let maxHeight = superViewHeight - self.tableView.frame.minY
              var frame = self.tableView.frame
              frame.size.height = min(self.tableView.contentSize.height, maxHeight)
              self.tableView.frame = frame
              }
              }





              share|improve this answer


























                0












                0








                0







                Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:



                DispatchQueue.main.async {
                if let superViewHeight = self.tableView.superView?.bounds.maxY {
                let maxHeight = superViewHeight - self.tableView.frame.minY
                var frame = self.tableView.frame
                frame.size.height = min(self.tableView.contentSize.height, maxHeight)
                self.tableView.frame = frame
                }
                }





                share|improve this answer













                Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:



                DispatchQueue.main.async {
                if let superViewHeight = self.tableView.superView?.bounds.maxY {
                let maxHeight = superViewHeight - self.tableView.frame.minY
                var frame = self.tableView.frame
                frame.size.height = min(self.tableView.contentSize.height, maxHeight)
                self.tableView.frame = frame
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 7:54









                vauxhallvauxhall

                725611




                725611























                    0














                    Code Work :



                    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

                    @IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
                    @IBOutlet weak var tblView: UITableView!

                    var tblMaxHeight : CGFloat = 50

                    override func viewDidLoad() {
                    super.viewDidLoad()

                    let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height

                    tblMaxHeight = self.view.frame.size.height - 40 - navHeight

                    }


                    override func viewDidLayoutSubviews(){

                    tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
                    }


                    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    return 24
                    }

                    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

                    cell?.textLabel?.text = "Row: #(indexPath.row)"

                    return cell!
                    }


                    }


                    Constraints to tableView :



                    enter image description here



                    Output :



                    enter image description here



                    enter image description here






                    share|improve this answer






























                      0














                      Code Work :



                      class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

                      @IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
                      @IBOutlet weak var tblView: UITableView!

                      var tblMaxHeight : CGFloat = 50

                      override func viewDidLoad() {
                      super.viewDidLoad()

                      let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height

                      tblMaxHeight = self.view.frame.size.height - 40 - navHeight

                      }


                      override func viewDidLayoutSubviews(){

                      tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
                      }


                      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                      return 24
                      }

                      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                      let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

                      cell?.textLabel?.text = "Row: #(indexPath.row)"

                      return cell!
                      }


                      }


                      Constraints to tableView :



                      enter image description here



                      Output :



                      enter image description here



                      enter image description here






                      share|improve this answer




























                        0












                        0








                        0







                        Code Work :



                        class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

                        @IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
                        @IBOutlet weak var tblView: UITableView!

                        var tblMaxHeight : CGFloat = 50

                        override func viewDidLoad() {
                        super.viewDidLoad()

                        let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height

                        tblMaxHeight = self.view.frame.size.height - 40 - navHeight

                        }


                        override func viewDidLayoutSubviews(){

                        tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
                        }


                        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                        return 24
                        }

                        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

                        cell?.textLabel?.text = "Row: #(indexPath.row)"

                        return cell!
                        }


                        }


                        Constraints to tableView :



                        enter image description here



                        Output :



                        enter image description here



                        enter image description here






                        share|improve this answer















                        Code Work :



                        class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

                        @IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
                        @IBOutlet weak var tblView: UITableView!

                        var tblMaxHeight : CGFloat = 50

                        override func viewDidLoad() {
                        super.viewDidLoad()

                        let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height

                        tblMaxHeight = self.view.frame.size.height - 40 - navHeight

                        }


                        override func viewDidLayoutSubviews(){

                        tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
                        }


                        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                        return 24
                        }

                        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

                        cell?.textLabel?.text = "Row: #(indexPath.row)"

                        return cell!
                        }


                        }


                        Constraints to tableView :



                        enter image description here



                        Output :



                        enter image description here



                        enter image description here







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 23 '18 at 8:09

























                        answered Nov 23 '18 at 8:03









                        dahiya_boydahiya_boy

                        4,97411027




                        4,97411027























                            0














                            Put your UITableView inside a UIScrollView and add constraints like this:



                            Constraints to Table View



                            enter image description here



                            Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController



                            override func viewWillLayoutSubviews() {
                            super.updateViewConstraints()
                            self.tableViewHeightConstraint.constant = tableView.contentSize.height
                            }





                            share|improve this answer




























                              0














                              Put your UITableView inside a UIScrollView and add constraints like this:



                              Constraints to Table View



                              enter image description here



                              Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController



                              override func viewWillLayoutSubviews() {
                              super.updateViewConstraints()
                              self.tableViewHeightConstraint.constant = tableView.contentSize.height
                              }





                              share|improve this answer


























                                0












                                0








                                0







                                Put your UITableView inside a UIScrollView and add constraints like this:



                                Constraints to Table View



                                enter image description here



                                Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController



                                override func viewWillLayoutSubviews() {
                                super.updateViewConstraints()
                                self.tableViewHeightConstraint.constant = tableView.contentSize.height
                                }





                                share|improve this answer













                                Put your UITableView inside a UIScrollView and add constraints like this:



                                Constraints to Table View



                                enter image description here



                                Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController



                                override func viewWillLayoutSubviews() {
                                super.updateViewConstraints()
                                self.tableViewHeightConstraint.constant = tableView.contentSize.height
                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 23 '18 at 9:05









                                Daniyal RazaDaniyal Raza

                                26423




                                26423























                                    0














                                    I have solved a similar problem using the following method. You only need few lines of code.



                                    override func viewDidLoad() {
                                    super.viewDidLoad()
                                    setupTableView()
                                    }

                                    private func setupTableView() {
                                    tableView.estimatedRowHeight = 44.0
                                    tableView.rowHeight = UITableViewAutomaticDimension
                                    }


                                    Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.






                                    share|improve this answer




























                                      0














                                      I have solved a similar problem using the following method. You only need few lines of code.



                                      override func viewDidLoad() {
                                      super.viewDidLoad()
                                      setupTableView()
                                      }

                                      private func setupTableView() {
                                      tableView.estimatedRowHeight = 44.0
                                      tableView.rowHeight = UITableViewAutomaticDimension
                                      }


                                      Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        I have solved a similar problem using the following method. You only need few lines of code.



                                        override func viewDidLoad() {
                                        super.viewDidLoad()
                                        setupTableView()
                                        }

                                        private func setupTableView() {
                                        tableView.estimatedRowHeight = 44.0
                                        tableView.rowHeight = UITableViewAutomaticDimension
                                        }


                                        Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.






                                        share|improve this answer













                                        I have solved a similar problem using the following method. You only need few lines of code.



                                        override func viewDidLoad() {
                                        super.viewDidLoad()
                                        setupTableView()
                                        }

                                        private func setupTableView() {
                                        tableView.estimatedRowHeight = 44.0
                                        tableView.rowHeight = UITableViewAutomaticDimension
                                        }


                                        Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 23 '18 at 9:48









                                        jmsjms

                                        3378




                                        3378























                                            0














                                            You could create a custom UITableView :



                                            class AutomaticHeightTableView: UITableView {

                                            override var contentSize: CGSize {
                                            didSet {
                                            self.invalidateIntrinsicContentSize()
                                            }
                                            }

                                            override var intrinsicContentSize: CGSize {
                                            self.layoutIfNeeded()
                                            return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
                                            }

                                            }


                                            And then set your UITableView Class to AutomaticHeightTableView.



                                            This solution is inspired from an answer found on stackoverflow.






                                            share|improve this answer




























                                              0














                                              You could create a custom UITableView :



                                              class AutomaticHeightTableView: UITableView {

                                              override var contentSize: CGSize {
                                              didSet {
                                              self.invalidateIntrinsicContentSize()
                                              }
                                              }

                                              override var intrinsicContentSize: CGSize {
                                              self.layoutIfNeeded()
                                              return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
                                              }

                                              }


                                              And then set your UITableView Class to AutomaticHeightTableView.



                                              This solution is inspired from an answer found on stackoverflow.






                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                You could create a custom UITableView :



                                                class AutomaticHeightTableView: UITableView {

                                                override var contentSize: CGSize {
                                                didSet {
                                                self.invalidateIntrinsicContentSize()
                                                }
                                                }

                                                override var intrinsicContentSize: CGSize {
                                                self.layoutIfNeeded()
                                                return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
                                                }

                                                }


                                                And then set your UITableView Class to AutomaticHeightTableView.



                                                This solution is inspired from an answer found on stackoverflow.






                                                share|improve this answer













                                                You could create a custom UITableView :



                                                class AutomaticHeightTableView: UITableView {

                                                override var contentSize: CGSize {
                                                didSet {
                                                self.invalidateIntrinsicContentSize()
                                                }
                                                }

                                                override var intrinsicContentSize: CGSize {
                                                self.layoutIfNeeded()
                                                return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
                                                }

                                                }


                                                And then set your UITableView Class to AutomaticHeightTableView.



                                                This solution is inspired from an answer found on stackoverflow.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 23 '18 at 16:55









                                                Marwen DoukhMarwen Doukh

                                                41649




                                                41649






























                                                    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%2f53442384%2fhow-to-set-uitableview-height-accroding-to-its-content-height%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

                                                    Costa Masnaga

                                                    Fotorealismo

                                                    Sidney Franklin