将关闭按钮添加到底部工作表

发布于 2025-01-15 11:15:30 字数 1550 浏览 0 评论 0原文

在 Apple 关于工作表视图的文档中,他们演示了如何实例化一个视图控制器,该视图控制器显示底部工作表,右侧有一个“关闭”按钮。考虑下面的照片:

在此处输入图像描述

我很难复制这一点。当我在导航控制器中呈现工作表视图并显式添加关闭按钮时,我可以看到关闭按钮,但工作表变成了 .large() 制动器。他们的照片表明他们正在通过 .medium() 制动装置实现这一目标。 UISheetPresentationController 的文档没有显示其他替代方案。知道我做错了什么吗?

我的 SheetController:

override func viewDidLoad() {
    ....
    navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: .done, target: self, action: #selector(closeSheet))
    ....
}

在不同的控制器中,单击按钮即可启动sheetController 的呈现:

@objc func showTheSheet() {
    let updateController = MySheetController()
    if let sheet = updateController.sheetPresentationController {
        sheet.detents = [.medium()]
        sheet.largestUndimmedDetentIdentifier = nil
        sheet.prefersGrabberVisible = true
        sheet.prefersScrollingExpandsWhenScrolledToEdge = true
        sheet.prefersEdgeAttachedInCompactHeight = true
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true
    }
    let navController = UINavigationController(rootViewController: updateController)
    self.present(navController, animated: true, completion: nil)
}

In Apple's documentation on Sheet Views, they demonstrate how to instantiate a view controller that displays a Bottom Sheet with a "close" button on the right-hand side. Consider the photo below:

enter image description here

I am having a hard time replicating this. When I present the Sheet View in a NavigationController, and explicitly add a close button, I can see the close button but the sheet becomes a .large() detent. Their photo suggests they are achieving this with a .medium() detent. Documentation for UISheetPresentationController shows no other alternative. Any idea what I am doing wrong?

My SheetController:

override func viewDidLoad() {
    ....
    navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: .done, target: self, action: #selector(closeSheet))
    ....
}

In a different controller, a button click initiates the presentation of the sheetController:

@objc func showTheSheet() {
    let updateController = MySheetController()
    if let sheet = updateController.sheetPresentationController {
        sheet.detents = [.medium()]
        sheet.largestUndimmedDetentIdentifier = nil
        sheet.prefersGrabberVisible = true
        sheet.prefersScrollingExpandsWhenScrolledToEdge = true
        sheet.prefersEdgeAttachedInCompactHeight = true
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true
    }
    let navController = UINavigationController(rootViewController: updateController)
    self.present(navController, animated: true, completion: nil)
}

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

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

发布评论

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

评论(1

月亮坠入山谷 2025-01-22 11:15:30

啊,答案是将导航控制器包裹到床单中。愚蠢的错误,但可能对某人有帮助。

@objc func updateAction() {
    let updateController = MySheetController()
    let navController = UINavigationController(rootViewController: updateController)
    if let sheet = navController.sheetPresentationController {
        sheet.detents = [.medium()]
        sheet.largestUndimmedDetentIdentifier = nil
        sheet.prefersGrabberVisible = true
        sheet.prefersScrollingExpandsWhenScrolledToEdge = true
        sheet.prefersEdgeAttachedInCompactHeight = true
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true
    }
    self.present(navController, animated: true, completion: nil)
}

Ah, the answer is to wrap the nav controller into the sheet. Silly mistake, but might help someone.

@objc func updateAction() {
    let updateController = MySheetController()
    let navController = UINavigationController(rootViewController: updateController)
    if let sheet = navController.sheetPresentationController {
        sheet.detents = [.medium()]
        sheet.largestUndimmedDetentIdentifier = nil
        sheet.prefersGrabberVisible = true
        sheet.prefersScrollingExpandsWhenScrolledToEdge = true
        sheet.prefersEdgeAttachedInCompactHeight = true
        sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true
    }
    self.present(navController, animated: true, completion: nil)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文