Avoid popover adapting to fullscreen in horizontally compact environment

狂风中的少年 提交于 2020-07-21 06:01:27

问题


According to documentation,

In a horizontally compact environment, popovers adapt to the UIModalPresentationOverFullScreen presentation style by default.

See below image. In compact environment, popover appear from bottom and animate to top until it covers the entire screen.

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

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:

Thank you 🙇‍♂️

来源:https://stackoverflow.com/questions/62858550/avoid-popover-adapting-to-fullscreen-in-horizontally-compact-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!