Избегайте адаптации всплывающего окна к полноэкранному режиму в горизонтально компактной среде - PullRequest
3 голосов
/ 12 июля 2020

Согласно документации ,

В горизонтально компактной среде всплывающие окна по умолчанию адаптируются к стилю представления UIModalPresentationOverFullScreen.

См. Изображение ниже . В компактной среде всплывающие окна появляются снизу и анимируются вверх, пока не покрывают весь экран.

enter image description here

Is it possible to override this behaviour and have the popover only covering certain height of the screen as shown below?

enter image description here

Following code demonstrate the default behaviour of popover adapting to FullScreen presentation style in compact environment.

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .systemBackground
        
        let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(UIImage(systemName: "square.grid.2x2.fill"), for: .normal)
        button.addTarget(self, action: #selector(displayPopover), for: .touchUpInside)
        self.view.addSubview(button)
        
        NSLayoutConstraint.activate([
            button.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100),
            button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            button.widthAnchor.constraint(equalToConstant: 40),
            button.heightAnchor.constraint(equalToConstant: 40),
        ])
        
    }
    
    @IBAction func displayPopover(sender: UIButton!) {
        let popoverVC = PopoverViewController()
        popoverVC.preferredContentSize = CGSize(width: 300, height: 200)
        popoverVC.modalPresentationStyle = .popover
        popoverVC.popoverPresentationController?.sourceView = sender
        popoverVC.popoverPresentationController?.permittedArrowDirections = .up
        self.present(popoverVC, animated: true, completion: nil)
    }
    
    
}

class PopoverViewController: UIViewController {
    override func viewDidLoad() {
        self.view.backgroundColor = .systemGray
    }
}

Output:

введите описание изображения здесь

Спасибо ?‍♂️

...