片材下方的插入内容带有中等制动装置

发布于 2025-01-11 20:10:32 字数 374 浏览 4 评论 0原文

iOS 15 添加了 UISheetPresentationController,它允许您通过将 sheetPresentationController 的制动器设置为 [.medium()] 来实现半高工作表。然后,您可以将 LargestUndimmedDetentIdentifier 设置为 .medium,以允许与呈现的视图控制器和工作表进行交互,从而实现类似于地图和股票的界面。

我的问题是,如何检测何时呈现未变暗的工作表并获取其高度以便在底层屏幕中插入内容。例如,我想将内容插入添加到我的表格视图中,以便您可以滚动查看所有单元格。目前,工作表覆盖了底部单元格,使其无法与这些单元格交互 - 您只能点击工作表所在位置上方的单元格。

iOS 15 added UISheetPresentationController which allows you to implement a half-height sheet by setting sheetPresentationController's detents to [.medium()]. You can then set the largestUndimmedDetentIdentifier to .medium to allow interacting with the presenting view controller and the sheet so you can achieve an interface similar to Maps and Stocks.

My question is, how could you detect when an undimmed sheet is presented and get its height in order to inset content in the underlying screen(s). For example, I want to add content insets to my table view so that you can scroll to see all cells. Currently, the sheet covers up the bottom cells making it impossible to interact with those - you can only tap the ones that lie above where the sheet rests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2025-01-18 20:10:32

为此,请设置 sheetPresentationController.delegate 并采用 UISheetPresentationControllerDelegate 协议。这将通知您何时出现和何时消失。然后,您可以在 animateChanges 块中根据需要对内容进行动画移动。这是一个示例:

extension ViewController: UISheetPresentationControllerDelegate {

    func presentationController(_ presentationController: UIPresentationController, willPresentWithAdaptiveStyle style: UIModalPresentationStyle, transitionCoordinator: UIViewControllerTransitionCoordinator?) {
        updateContentsForPresentedSheet(isPresented: true)
        (presentationController as? UISheetPresentationController)?.animateChanges {
            view.layoutIfNeeded()
        }
    }
    
    func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
        updateContentsForPresentedSheet(isPresented: false)
        (presentationController as? UISheetPresentationController)?.animateChanges {
            view.layoutIfNeeded()
        }
    }
}
private func updateContentsForPresentedSheet(isPresented: Bool) {
    bottomConstraint.constant = isPresented ? view.bounds.height / 2 : 0
}

请注意,中等制动高度看似不确定,但接近屏幕高度的一半。如果您需要精确调整界面,iOS 16 添加了提供自定义制动器的功能,您可以在其中指定所需的确切高度。这是一个例子:

viewController.sheetPresentationController?.detents = [.custom(resolver: { [weak self] context in
    guard let self else { return nil }
    // Note the system automatically accommodates the safe area so we'll subtract those insets
    return self.view.bounds.height / 2 - self.view.safeAreaInsets.bottom
})]

To do this, set the sheetPresentationController.delegate and adopt the UISheetPresentationControllerDelegate protocol. This will inform you when it will present and did dismiss. You can then animate moving your content as appropriate in an animateChanges block. Here's an example:

extension ViewController: UISheetPresentationControllerDelegate {

    func presentationController(_ presentationController: UIPresentationController, willPresentWithAdaptiveStyle style: UIModalPresentationStyle, transitionCoordinator: UIViewControllerTransitionCoordinator?) {
        updateContentsForPresentedSheet(isPresented: true)
        (presentationController as? UISheetPresentationController)?.animateChanges {
            view.layoutIfNeeded()
        }
    }
    
    func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
        updateContentsForPresentedSheet(isPresented: false)
        (presentationController as? UISheetPresentationController)?.animateChanges {
            view.layoutIfNeeded()
        }
    }
}
private func updateContentsForPresentedSheet(isPresented: Bool) {
    bottomConstraint.constant = isPresented ? view.bounds.height / 2 : 0
}

Note the medium detent height is seemingly indeterminate but close to half the screen height. If you need to precisely adjust your interface, iOS 16 adds the ability to provide a custom detent where you specify the exact height desired. Here's an example:

viewController.sheetPresentationController?.detents = [.custom(resolver: { [weak self] context in
    guard let self else { return nil }
    // Note the system automatically accommodates the safe area so we'll subtract those insets
    return self.view.bounds.height / 2 - self.view.safeAreaInsets.bottom
})]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文