ios - 避免弹出窗口在水平紧凑的环境中适应全屏

标签 ios swift popover

根据 documentation ,

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

见下图。在紧凑的环境中,弹出窗口从底部出现并动画到顶部,直到它覆盖整个屏幕。

enter image description here

是否可以覆盖此行为并让弹出窗口仅覆盖屏幕的特定高度,如下所示?

enter image description here

以下代码演示了弹出窗口在紧凑环境中适应全屏显示样式的默认行为。

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
    }
}

输出:

enter image description here

谢谢🙇‍♂️

最佳答案

此功能现在适用于 iOS 15。

观看 WWDC2021 短片 video

here 下载 WWDC 示例项目

 @IBAction func showImagePicker(_ sender: UIBarButtonItem) {
    
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.modalPresentationStyle = .popover
    if let popover = imagePicker.popoverPresentationController {
        popover.barButtonItem = sender
        
        let sheet = popover.adaptiveSheetPresentationController
        sheet.detents = [.medium(), .large()]
        sheet.largestUndimmedDetentIdentifier = .medium
        sheet.prefersScrollingExpandsWhenScrolledToEdge = false
        sheet.prefersEdgeAttachedInCompactHeight = true 
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true
    }
    
    present(imagePicker, animated: true, completion: nil)
}

关于ios - 避免弹出窗口在水平紧凑的环境中适应全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62858550/

相关文章:

ios - 节点的位置在物理模拟过程中保持不变

ios - 是否有关于如何从 Swift 中的类构造函数中提取参数的最佳实践?

ios - 从 App 打开 map 返回 Nil 作为自定义点注释的位置标题

angularjs - 如何在 AngularJS 中通过 ng-show 触发 Bootstrap 弹出窗口

javascript - 从另一个 HTML 文件 Bootstrap 弹出窗口数据

iOS 获取一个 UITableViewCellAccessoryDe​​tail 的 View

iphone - 如何从表格 View 单元格中搜索数据?

ios - mdl-js-ripple-effect 破坏了 iOS 13.2(测试版)上的 MDL 按钮

iOS 8 UITableView 背景色外观

html - 使用 CSS 为以下设计创建布局