es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Teclado en UISheetPresentationController empuja la vista hacia arriba.

Tengo un controlador de vista que empujo a través de sheetPresentationController:

    let addRSSSheetVC = AddRSSSheetViewController()
    let navigationController = UINavigationController(rootViewController: addRSSSheetVC)

    if let sheet = navigationController.sheetPresentationController {
        sheet.detents = [.medium()]
    }

    present(navigationController, animated: true)

Y cuando se presenta la hoja, se ve así:

Descripción de la imagen

Pero cuando hago clic en el campo de texto y aparece el teclado, empuja la vista de la hoja hacia arriba:

Descripción de la imagen

¿Cómo puedo presentar el teclado por encima del controlador de vista presentado sin empujar la página?

Tags:  , , ,

Answer

  1. Avatar for davy.ai

    Para presentar el teclado por encima del controlador de vista presentado sin empujar la página, puedes ajustar la propiedad presentationController?.frameOfPresentedViewInContainerView del sheetPresentationController.

    1. Primero, cumple con el protocolo UIAdaptivePresentationControllerDelegate en el controlador de vista presentador.
    2. Establece el delegado del controlador de presentación al controlador de vista presentador.

    navigationController.presentationController?.delegate = self
    
    1. Implementa el método presentationController(_:willPresentAdaptivePresentationStyle:traitCollection:) para ajustar el marco del controlador de vista presentado cuando se muestra el teclado.
    func presentationController(_ controller: UIPresentationController, willPresentAdaptivePresentationStyle style: UIModalPresentationStyle, traitCollection: UITraitCollection?) {
        if let presentedVC = controller.presentedViewController as? AddRSSSheetViewController {
            presentedVC.view.frame = CGRect(x: 0, y: 0, width: controller.containerView?.frame.width ?? 0, height: controller.containerView?.frame.height ?? 0)
            presentedVC.view.layoutIfNeeded()
        }
    }
    
    1. Finalmente, establece la propiedad sheetPresentationController?.shouldAdjustToInitialKeyboardPosition en true para evitar que la página se empuje hacia arriba cuando se muestra el teclado.
    if let sheet = navigationController.sheetPresentationController {
        sheet.detents = [.medium()]
        sheet.shouldAdjustToInitialKeyboardPosition = true
    }
    

    Esto permitirá presentar el teclado por encima del controlador de vista existente sin empujar la página hacia arriba.

Comments are closed.