我正在尝试在我的标签栏控制器中添加一个添加按钮,单击此按钮将显示一个表视图模式。我希望模态小于全屏,背景视图模糊。任何人都可以帮我这个吗?
相关代码如下:
的AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return tue
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is AddViewController {
if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "AddModal"){
tabBarController.present(newVC, animated: true)
return false
}
}
return true
}
}
模态视图控制器:
class AddViewController: UIViewController {
var list = [
"item1",
"item2",
"item3",
"item4"
]
var text: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view = UITableView(frame: .zero)
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
}
extension AddViewController {
private var tableView: UITableView {
return self.view as! UITableView
}
}
extension AddViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tabBarController?.dismiss(animated: false, completion: {
return
})
}
}