0

enter image description here

I have a UITableView cells that won't follow it's parent width. I found so many codes for how to change the height, however I couldn't found codes/ways to change UITableView cells to same as it parents.

Some of answer told that it would follow automatically. But in mine, the problem is the width can't follow device's width. Is there anyone that know how to change it?

Firda Sahidi
  • 1,227
  • 1
  • 11
  • 22
  • what do u mean by parent ? – vivekDas Nov 27 '18 at 09:52
  • Please share your code by [edit](https://stackoverflow.com/posts/53496893/edit)-ing the question. – Rakesha Shastri Nov 27 '18 at 09:53
  • Don't forget to share what are you trying to achieve. – Ratul Sharker Nov 27 '18 at 09:59
  • @vivekDas UIStoryboard / my device width – Firda Sahidi Nov 27 '18 at 10:03
  • @RakeshaShastri I already edit it :) – Firda Sahidi Nov 27 '18 at 10:03
  • @RatulSharker done – Firda Sahidi Nov 27 '18 at 10:04
  • 2
    for width you don't have to do anything bro !! according to the tableview width cell width set automatically ! – vivekDas Nov 27 '18 at 10:06
  • @vivekDas is right, just shrink the `UITableView` width wise, according to your requirement. – Ratul Sharker Nov 27 '18 at 10:07
  • What about the constraints in the cell? If you have set your UI Correctly then the cell will take the entire width of the tableview. This is how it is designed to work. – Alan S Nov 27 '18 at 10:13
  • It doesn't work if I change the device screen. It doesn't follow the device screen's width. – Firda Sahidi Nov 27 '18 at 10:29
  • @AlanSarraf I think I already set my UI Correctly. When I change the width, the item in the cell just adjust it. The problem is when I test in different screen size, it won't follow the screen size. – Firda Sahidi Nov 27 '18 at 10:30
  • Do you mean changing device orientation (Portrait to landscape) or it works on a smaller device but on a larger device it doesn't take the full width? – Alan S Nov 27 '18 at 10:31
  • @AlanSarraf, no, I mean the device, example from iPhone XR to iPhone 6 – Firda Sahidi Nov 27 '18 at 10:32
  • Then make sure that your table view is constrained correctly. If you want it to take the full width, instead of giving the tableview a fixed width constraint, give it leading and trailing constraints with value 0 – Alan S Nov 27 '18 at 10:34
  • @AlanSarraf I couldn't add leading and trailing to it. When I ctrl and drag it, it only shows the option to change the height or width and aspect ratio. – Firda Sahidi Nov 27 '18 at 10:38
  • You don't add leading and trailing with ctrl drag. You have to use the Pin tool. Follow this link and check the Pin Tool section: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html – Alan S Nov 27 '18 at 10:39

1 Answers1

8

The UITableviewCell width will always be the same width of the UITableView.

If you want to set the width of the cell same as the parent of UITableView, You should set the width of the UITableView same as that of parent

If you want to set the width of UITableView the same as it's parent view, set the trailing and leading constraints of your UITableView to it's superview with value 0

you need to remove the width constraint and aspect ratio that you have added to it. Instead of ctrl and drag, select the tableview and click the pin button and set the constraints as shown in this image. Remember the trailing and leading values must be 0 for setting the width as same as parent view

Amal T S
  • 3,327
  • 2
  • 24
  • 57