Ramcharan Reddy Ramcharan Reddy - 1 year ago 79
iOS Question

How to restrict the size of table view which is on left side of the split view controller?

enter image description hereI have a UISplitViewController. So I want to see my master view(i.e table view) upto the size of its content (consider it have only 3 element's).
I tried With self.tableview.contentsize but did not succeeded. Please help me finding solution.

Each cell's Height is 44 .
This is the Code That I wrote.

class AccountTableViewController: UITableViewController {
var filterList = [String]()
var selectedIndex = -1

override func viewDidLoad() {
filterList = ["All Accounts","Business Accounts","Person Accounts"]

self.tableView.contentSize = CGSize(width: 600, height: 44*3);

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("accountCell", forIndexPath: indexPath)

cell.textLabel?.text = filterList[indexPath.row]
return cell

Answer Source

I grabbed Ray Wenderlich's example project (https://www.raywenderlich.com/94443/uisplitviewcontroller-tutorial-getting-started) as a starting point. That's what you see modified in the pictures below.

In Interface Builder, first make sure your set to use AutoLayout: enter image description here

Then drag a UIView under the prototype cell (I set the background to orange so you can see where it is), and make the size whatever you like: enter image description here

I added some fields to the to the new UIView that we just created, and I added constraints so you could see them move based on the content at runtime. (You'll notice the storyboard view controller as a much larger width in IB than at runtime.) Here's the constraints I added: enter image description here

Finally, run the app--no need to do anything in code: enter image description here

Once you get this basic part working, then you can configure the size of the view in code. You can design the view in IB if you want, or do it in code--whatever you prefer.

Here's an example with just one row in the table:

enter image description here

And here's an example with no rows in the table:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download