-1

I'm using a UITableViewController with static cells and I want to make it so that the cells do not take up the entirety of the view. I want something more akin to this image: https://i.stack.imgur.com/4nO09.png

I can't quite figure out how to do so. I've been able to change the height thanks to self.tableView.contentInset, but I'm not sure how to change the width.

How would I do this?

EDIT:

Here's my Code for Fay007 as well as an image.

import UIKit

class ContactFormViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Sets the background images
    if let patternImage = UIImage(named: "Pattern") {
        view.backgroundColor = UIColor(patternImage: patternImage)
    }
}

override func viewWillAppear(_ animated: Bool) {
    let numberOfRows: CGFloat = CGFloat(self.tableView.numberOfRows(inSection: 0))
    let headerHeight: CGFloat = (self.view.frame.size.height - (self.tableView.rowHeight * numberOfRows)) / numberOfRows

    self.tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0)
        }

https://i.stack.imgur.com/w45xe.jpg

The first image I linked has its cells away from the left/right edges, as in my comment I explained I believe they did using autolayout. Since the tableview is a subview of the UIView of the UIViewController, I believe one would be able to assure that. however, when using a UITableViewController, which is required to use static cells in a UITableView, there is no UIView parent.

NappyXIII
  • 195
  • 2
  • 17
  • http://stackoverflow.com/a/33931591/2356808 same content from this answer? and it also implemented the solution! – Fay007 Jan 08 '17 at 10:00
  • Possible duplicate of [How to add spacing between UITableViewCell](http://stackoverflow.com/questions/6216839/how-to-add-spacing-between-uitableviewcell) – Fay007 Jan 08 '17 at 10:01
  • @Fay007 That's where I got the image. And that solution solves the problem in a way that I don't think is possible considering static table cells must use a UITableViewController, not a UIViewController as well as it uses sections rather than rows. I could be wrong because I'm trying to figure this out, but that solution did not seem to be the answer I was looking for. It seems to fix height issues, not width issues, which they merely did by (assumingly) changing the way the tableview was set within the UIView. – NappyXIII Jan 08 '17 at 10:12
  • it has nothing to do with static cell nor implementing uitableviewcontroller instead of uiviewcontroller! and i didn't understand the term "width issue"! show me what you have done so far so that i can post a proper answer – Fay007 Jan 08 '17 at 10:18
  • I've added code an an image. But I don't think you are clearly reading the question. I am trying to add padding to my static cell so that the width is not the whole view. The question you've pointed me to has nothing to do with the distance of the tableViewCell to either side of the screen. It has to do with spacing between cells. Completely irrelevant question. – NappyXIII Jan 08 '17 at 12:00

1 Answers1

0

One easy way of doing this is as follows: In storyboard, or interface builder, add a UIView subview to the UITableviewcell. Create constraints that define your desired distance of this subview to the edges of the cell.

To add rounded corners, you can do so within awakeFromNib by setting the cornerRadius of the subview's layer property to your desired radius.

cohen72
  • 2,830
  • 29
  • 44