iOS - стек полноэкранного viewController при представлении другого viewController - PullRequest
0 голосов
/ 24 февраля 2020

У меня есть UITabBarViewController с двумя вкладками. Я хочу представить ViewController в полноэкранном режиме на одной из вкладок. Для этого я использовал следующий код.

    let navCtrl = UINavigationController(rootViewController: eventViewController)
    navCtrl.modalPresentationStyle = .fullScreen        
    self.navigationController?.present(navCtrl, animated: true)

Это работает. И EventViewController полноэкранный. Однако при представлении другого viewController в EventViewController, EventViewController по-прежнему полноэкранный. Но я хочу, чтобы он уменьшился в размере и увеличился как обычно (как на картинке). Для этого я изменил modalPresentationStyle на overCurrentContext.

    let navCtrl = UINavigationController(rootViewController: eventViewController)
    navCtrl.modalPresentationStyle = .overCurrentContext        
    self.navigationController?.present(navCtrl, animated: true)

Это так, но это вызывает другую проблему: если я изменю вкладки и отклоню EventViewController, представление viewController будет black как описано в этом вопросе (ни один из ответов не помог) .


enter image description here

По сути, я хочу, чтобы EventController был полноэкранным, но уменьшался в размерах при представлении в нем другого контроллера. Как это сделать?

Обновление

Простой проект с той же проблемой.

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let ctrl = TabZeroViewController()
        ctrl.tabBarItem.image = UIImage(named: "archived-task")
        ctrl.tabBarItem.title = "One"


        let test = TabOneViewController()
        test.tabBarItem.image = UIImage(named: "Test")
        test.tabBarItem.title = "Test"

        let tabBarList = [ctrl, test ]

        self.viewControllers = tabBarList.map {
            let nav = UINavigationController(rootViewController: $0)
            nav.interactivePopGestureRecognizer?.isEnabled = true
            return nav
        }
    }
}



class TabZeroViewController: UITableViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self

        self.view.backgroundColor = .white
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = ModalTableViewController()

        let nav = UINavigationController(rootViewController: ctrl)
        nav.modalPresentationStyle = .fullScreen

        self.navigationController?.present(nav, animated: true)
    }

}

class ModalTableViewController: UITableViewController {
    override func viewDidLoad() {
        self.view.backgroundColor = .red
        let button = UIButton()
        button.setTitle("Cancel", for: .normal)
        button.addTarget(self, action: #selector(dismissModal), for: .allEvents)
        let item = UIBarButtonItem()
        item.customView = button
        self.navigationItem.leftBarButtonItem = item
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    @objc func dismissModal() {
        self.dismiss(animated: true, completion: nil)
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text = "Event"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = EventViewController()
        let nav = UINavigationController(rootViewController: ctrl)
         nav.modalPresentationStyle = .overCurrentContext
        self.navigationController?.present(nav, animated: true)
    }
}


class TabOneViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}




class EventViewController: UITableViewController {

    override func viewDidLoad() {
        self.view.backgroundColor = .red
        let button = UIButton()
        button.setTitle("Cancel", for: .normal)
        button.addTarget(self, action: #selector(dismissModal), for: .allEvents)
        let item = UIBarButtonItem()
        item.customView = button
        self.navigationItem.leftBarButtonItem = item
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    @objc func dismissModal() {
        self.dismiss(animated: true, completion: nil)
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text = "Event"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = EventViewController()
        let nav = UINavigationController(rootViewController: ctrl)
        self.navigationController?.present(nav, animated: true)
    }
}

Добавьте этот код в willConnectTo из SceneDelegate.

 if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = TabBarController()
        self.window = window
        window.makeKeyAndVisible()
    }

Пока вы находитесь на первой вкладке, выберите ячейку таблицы, чтобы открыть ModalTableViewController. А потом смените вкладки и отклоните ModalTableViewController.

Ответы [ 2 ]

0 голосов
/ 05 марта 2020

Попробуйте этот код для представления экрана модально:

 func getImageFromView() -> UIImage {

        let layer = UIApplication.shared.keyWindow?.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer?.frame.size ?? CGSize.zero, false, scale)
        if let context = UIGraphicsGetCurrentContext() {
            layer?.render(in: context)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image ?? UIImage()
        }
        return UIImage()
    }


/// This is the method to present  screen modally
/// - parameter controller: controller instance on which screen will be presented
func presentScreenModally(controller: UIViewController, animated: Bool) {
    let loginController  = UIStoryboard.loadLoginViewController()//Get instance of controller form storyboard
    loginController.bgTranParentImg = getImageFromView()

    let bgImage = getImageFromView()
    let presentationStyleViewController = UIStoryboard.loadPresentationStyleController()// This is another controller, which I am pasting below
    presentationStyleViewController.bgimage = bgImage
    presentationStyleViewController.loginController = loginController
    presentationStyleViewController.addChild(loginController)
    controller.view.window?.addSubview(presentationStyleViewController.view)
    loginController.view.frame = presentationStyleViewController.containerView.bounds
    presentationStyleViewController.containerView.addSubview(loginController.view)

    let navigationController = UINavigationController(rootViewController: presentationStyleViewController)
    navigationController.navigationBar.isHidden = true
    navigationController.modalPresentationStyle = .fullScreen
    controller.navigationController?.present(navigationController, animated: animated, completion: nil)
}

PresentationStyleViewController class:

class PresentationStyleViewController: UIViewController {

    @IBOutlet var containerView: UIView!
    @IBOutlet var containeTopConstraint: NSLayoutConstraint!
    @IBOutlet var containerBottomConstraint: NSLayoutConstraint!
    @IBOutlet var backgroundImage: UIImageView!

    var bgimage: UIImage?
    let topPadding: CGFloat = 30
    var loginController: LoginViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.uiSetup()
    }

    override func viewDidAppear(_ animated: Bool) {
        restorePopup()
    }

    /// Initial UI setup
    func uiSetup() {
        containeTopConstraint.constant = self.view.frame.size.height
        backgroundImage.image = bgimage
    }

    @IBAction func panGesture(_ sender: UIPanGestureRecognizer) {
        guard let piece = sender.view else {return}
        let translation = sender.translation(in: piece.superview)
        containeTopConstraint.constant = translation.y >= topPadding ? translation.y : topPadding

        if sender.state == .ended || sender.state == .cancelled {
            if containeTopConstraint.constant > self.view.frame.size.height/4 && translation.y > 0 {
                self.dismissPopup()
            } else {
                self.restorePopup()
            }
        }
    }

    /// Dismisses popup and controller
    func dismissPopup() {
        containeTopConstraint.constant = self.view.frame.size.height
        UIView.animate(withDuration: 0.3,
                       animations: {
                        self.view.layoutIfNeeded()
        }, completion: { (_) in
            self.loginController?.btnClick_cross(UIButton())
            self.dismiss(animated: false)
        })
    }

    /// Restores popup at initial position
    func restorePopup() {
        containeTopConstraint.constant = topPadding
        UIView.animate(withDuration: 0.3,
                       animations: {
                        self.view.layoutIfNeeded()
        }, completion: nil)
    }
}
0 голосов
/ 04 марта 2020

Как, например, проект - представление в полноэкранном режиме скрывает TabBar. Но я немного изменил код, чтобы предложить рабочее решение. Возможно, вы захотите немного изменить его, но я надеюсь, что это приведет вас в правильном направлении :)

На самом деле нужно было отклонить ModalTableViewController, чтобы избежать черного экрана.

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let ctrl = TabZeroViewController()
        ctrl.tabBarItem.image = UIImage(named: "archived-task")
        ctrl.tabBarItem.title = "One"


        let test = TabOneViewController()
        test.tabBarItem.image = UIImage(named: "Test")
        test.tabBarItem.title = "Test"

        let tabBarList = [ctrl, test ]

        let viewControllers: [UIViewController] = tabBarList.map {
            let nav = UINavigationController(rootViewController: $0)
            nav.interactivePopGestureRecognizer?.isEnabled = true
            nav.tabBarItem = $0.tabBarItem
            return nav
        }

        self.setViewControllers(viewControllers, animated: false)
    }

    override var selectedViewController: UIViewController? {
        get {return super.selectedViewController}
        set {
            if super.selectedViewController?.presentedViewController != nil {
                super.selectedViewController?.dismiss(animated: false, completion: nil)
            }
            super.selectedViewController = newValue
        }
    }
}



class TabZeroViewController: UITableViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self

        self.view.backgroundColor = .white
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = ModalTableViewController()

        let nav = UINavigationController(rootViewController: ctrl)
        nav.modalPresentationStyle = .currentContext

        self.present(nav, animated: true)
    }

}

class ModalTableViewController: UITableViewController {
    override func viewDidLoad() {
        self.view.backgroundColor = .red
        let button = UIButton()
        button.setTitle("Cancel", for: .normal)
        button.addTarget(self, action: #selector(dismissModal), for: .allEvents)
        let item = UIBarButtonItem()
        item.customView = button
        self.navigationItem.leftBarButtonItem = item
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    @objc func dismissModal() {
        self.presentingViewController?.dismiss(animated: false, completion: nil)
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text = "Event"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = EventViewController()
        let nav = UINavigationController(rootViewController: ctrl)
         nav.modalPresentationStyle = .fullScreen
        self.navigationController?.present(nav, animated: true)
    }
}


class TabOneViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
    }
}




class EventViewController: UITableViewController {

    override func viewDidLoad() {
        self.view.backgroundColor = .red
        let button = UIButton()
        button.setTitle("Cancel", for: .normal)
        button.addTarget(self, action: #selector(dismissModal), for: .allEvents)
        let item = UIBarButtonItem()
        item.customView = button
        self.navigationItem.leftBarButtonItem = item
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    @objc func dismissModal() {
        self.dismiss(animated: true, completion: nil)
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text = "Event"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = EventViewController()
        let nav = UINavigationController(rootViewController: ctrl)
        self.navigationController?.present(nav, animated: true)
    }
}

Удачи!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...