0

after adding this codes to appDelegate inside didFinishLaunchingWithOptions function:

self.window = UIWindow(frame: UIScreen.main.bounds)
let mainView = PageViewController(coder: NSCoder()) 
let nav1 = UINavigationController(rootViewController: mainView!)
nav1.navigationBar.isTranslucent = false
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()

tableView height is not looking good, some part of tableView(cells) not fitting screen

my codes for implementing tableView:

let myTableView = UITableView(frame: CGRect(x: 0, y: 40, width: screenSize.width, height: screenSize.height-40))
myTableView.bounces = false
myTableView.tableFooterView = UIView()
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
myTableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cellID2")
myTableView.dataSource = self
myTableView.delegate = self

for show all part of tableView in screen i have to change the first line above code to this:

let myTableView = UITableView(frame: CGRect(x: 0, y: 40, width: screenSize.width, height: screenSize.height-90))

why this is happening and what should i do to solve this problem thanks

behrad
  • 596
  • 1
  • 5
  • 19

1 Answers1

1

Try something like this but don't forget to add myTableView as a subview before you set anchors:

            myTableView.translatesAutoresizingMaskIntoConstraints = false
            myTableView.leftAnchor.constraint(equalTo: myTableView.superview!.leftAnchor).isActive = true
            myTableView.rightAnchor.constraint(equalTo: myTableView.superview!.rightAnchor).isActive = true
            myTableView.topAnchor.constraint(equalTo: myTableView.superview!.topAnchor).isActive = true
            myTableView.bottomAnchor.constraint(equalTo: myTableView.superview!.bottomAnchor).isActive = true
Nazar Lisovyi
  • 311
  • 1
  • 7
  • could you please tell me why this is happening – behrad Apr 04 '18 at 14:52
  • is there another way to do this? i mean using navigationController and tableView inside viewController – behrad Apr 04 '18 at 14:56
  • Could you please provide screenshot of what do you have on screen with your implementation, so I can figure out whats wrong? You made some weird insets on top and on bottom, idk why :( – Nazar Lisovyi Apr 04 '18 at 14:57
  • "is there another way to do this? i mean using navigationController and tableView inside viewController" - I didn't quite get that – Nazar Lisovyi Apr 04 '18 at 14:58
  • i define UINavigationController in appDelegate programmatically for UIViewController that contain UITableView , your solution is solve my problem but in objective c i never see this problem , is there another way to add UINavigationController to UIViewController that contain UITableView – behrad Apr 04 '18 at 15:09
  • If you still wanna to init that without using constraints you can try this let myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height - nav1.navigationBar.bounds.height )) – Nazar Lisovyi Apr 04 '18 at 15:13