In my swift code below i wan the tableview cell to be pushed to the margins exactly like the cells left side. In the photo below you can see the margins of the tableview cell on the right side are a lot bigger than it its on the side of the left. Please make them equal.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
var numberOfRows = 3
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { numberOfRows }
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 118
}
var btn = UIButton()
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setTableVIew()
}
func setTableVIew(){
let VCframe = self.view.frame
let height = VCframe.height * 0.8
let widthx = VCframe.width
tableView.frame = CGRect(x: 0, y: 0, width: widthx, height: height)
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.backgroundColor = .blue
tableView.register(customtv.self, forCellReuseIdentifier: "cell")
}
}
class customtv: UITableViewCell {
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
view.backgroundColor = .green
print(self.frame.width)
return view
}()
override func layoutSubviews() {
backView.clipsToBounds = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(animated, animated: true)
addSubview(backView)
}
}
Update backView frame in layoutSubviews
class customtv: UITableViewCell {
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
view.backgroundColor = .green
print(self.frame.width)
return view
}()
override func layoutSubviews() {
backView.clipsToBounds = true
backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
}
they will become equall width cell ... if you want to give margin ... give it to your tableview instead of cell ...
tableView.frame = CGRect(x: 10, y: 0, width: widthx - 20, height: height)