ios - 根据选择的选项以不同的 View 呈现 View Controller : Swift 4

标签 ios iphone swift uiview viewcontroller

我将尽力解释我的问题,所以提前致谢。

我通常通过让每个 View Controller 代表不同的 View 来构建具有许多 View Controller 的应用程序。现在,我正在创建一个具有许多不同 View 的大型应用程序。我不想在我的应用程序中有 15 个以上的 viewController。我宁愿让一个 View Controller 继承不同的 UIView。

例如,如果在我的菜单栏上选择“设置”选项。然后我将被带到 selectionController 并让 SelectionController 继承名为 Settings View

的 UIView

目前,我可以导航到我的 settingsController 并让导航栏代表所选的选项。但我该如何继承 SettingsView 呢?

家庭 Controller

class HomeController: UIViewController {
    lazy var menu: Menu = {
        let menuLauncher = Menu()
        menuLauncher.homeController = self
        return menuLauncher
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Home"
            view.backgroundColor = Color.blue
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "menu"), style: .plain, target: self, action: #selector(displayMenu))
    }

    func displayController(menuOption: MenuOption) {
        let selectionController = UIViewController()
        selectionController.navigationItem.title = menuOption.name
        selectionController.view.backgroundColor = Color.blue
        navigationController?.navigationBar.tintColor = UIColor.white
        navigationController?.pushViewController(selectionController, animated: true)
    }

    @objc func displayMenu() {
        menu.displayMenu()
    }
}

菜单 View

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menuOptions.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
    let menuOption = menuOptions[indexPath.row]
    cell.menuOption = menuOption
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 30)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let menuOption = self.menuOptions[indexPath.row]
    print(menuOption.name)
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.blackView.alpha = 0

        if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
            let width = window.frame.width - 150

            self.menuView.frame = CGRect(x: -width, y: 0, width: 0, height: window.frame.height)
        }

    }) { (completed: Bool) in


//THIS IS WHERE I CALL MY FUNCTION TO SET UP THE SELECTIONCONTROLLER
        self.homeController?.displayController(menuOption: menuOption)
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)

    setUpView()
    setUpCollectionView()

}

选择选项后(以索引路径的形式),并且菜单动画完成后。我在 HomeController 中调用我的函数 displayController 并设置新 Controller 。

我如何实现一个 View ?如果我选择了“Settings”,我怎样才能让selectionController也显示SettingsView

最佳答案

您可以向现有 View 添加 subview :

//The container view can be in the vc with constraints already setup.
//Or you can use the vc's default view.
enum MyView: Int { case settingsView = 0, otherView }

//Add view
let view = UIView(frame: containerView.bounds)
view.tag = MyView.settingsView.rawValue
containerView.addSubview(view)

//Remove view
if let remove = containerView.viewWithTag(MyView.settingsView.rawValue) {
    remove.removeFromSuperview()
}

关于ios - 根据选择的选项以不同的 View 呈现 View Controller : Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50473686/

相关文章:

swift - 如何在 RxSwift 中观察对象的属性?

ios - 如何在 Swift 中关闭当前的 ViewController 并更改为新的 ViewController?

iphone - 如何在swift3中将分钟转换为小时?

iphone - 如何在 iPhone 的 TableCell 中获取 TextField 的位置?

Swift 的内存管理

swift - 在 Swift 中在哪里初始化?

ios - 在 iPhone 上阅读资源文本文件

iOS Fabric Crashlytics Answers Kit 不收集数据

iphone - 当表格重新出现时,UITableView 不会自动取消选择选定的行

iPhone 合成问题