将此代码添加到 didFinishLaunchingWithOptions 函数内的 appDelegate 后:
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 高度不好看,tableView(cells) 的某些部分不适合屏幕
我实现 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
为了在屏幕上显示 tableView 的所有部分,我必须将上面代码的第一行更改为:
let myTableView = UITableView(frame: CGRect(x: 0, y: 40, width: screenSize.width, height: screenSize.height-90))
为什么会这样,我应该怎么做才能解决这个问题,谢谢
最佳答案
尝试这样的事情,但不要忘记在设置 anchor 之前将 myTableView 添加为 subview :
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
关于swift: 将 navigationController 添加到 viewController 后,tableView 高度不适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653570/