Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set UITableView.Style to .insetGrouped based on device/traits

Tags:

ios

swift

ios13

I wanted to use the new iOS 13 table view style .insetGrouped only on iPad or based on size variation traits.

Sadly it's not possible to change the UITableView style after the init: https://developer.apple.com/documentation/uikit/uitableview/style

Changing it in the storyboard is also no option as there is no way to change it based on size classes.

I'm currently using this code, but nothing changed:

class DetailViewController: UITableViewController {

    […]

    override init(style: UITableView.Style) {

        if UIDevice.current.userInterfaceIdiom == .pad {
            super.init(style: .insetGrouped)
        } else {
            super.init(style: style)
        }
    }


    // Xcode forced me to include this as well?
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
like image 215
alexkaessner Avatar asked Aug 21 '19 11:08

alexkaessner


Video Answer


1 Answers

Create UIViewController with `

var tableView: UITableView!
init(...) {
    if ??? {
        self. tableView = UITableView(style:...)
    } else {
        self. tableView = UITableView(style:anotherStyle)
    }
    self.tableView.dataSource = self
    self.tableView.delegate = self
}

`

like image 102
WAMii Avatar answered Nov 14 '22 22:11

WAMii