簡體   English   中英

將 UIViewController 中的 UIView 顯示為上下文菜單預覽

[英]Show UIView from UIViewController as a context menu preview

我正在試驗 iOS 13 和新的 UIContextMenu API,當嘗試使用上下文菜單預覽時,我想顯示一張相對於被按下的 UITableRow 的小卡片。 I created a UIViewController in my storyboard, with a UIView inside that contains the info card itself (I made the UIView so that I can easily change its size with constraints). 我嘗試只通過這樣做來顯示它

@IBOutlet var infoCard = UIView!
override func loadView(){
    super.loadView()
    view = infoCard
    preferredContentSize = infoCard.bounds.size
}

但是,當顯示上下文菜單時,我看到了視圖,但卡片上方和下方都有黑色矩形,因為預覽的比例沒有改變。

如何以其他比率顯示此信息卡? 謝謝!

我終於設法做對了,我遇到的問題是 infoCard.bounds 將返回 storyboard 值,但我的視圖具有動態大小,並且 Swift 在繪制框架之前不會嘗試重新計算可見高度/寬度。 當 UIViewController 顯示在上下文菜單預覽中時,以下代碼可用於在 UIViewController 中呈現 UIView:

// All this is inside the view controller, with cardView being the displayed view
loadViewIfNeeded()
updateViewConstraints()

// This will force Swift to recalculate the view's size
cardView.drawHierarchy(in: cardView.bounds, afterScreenUpdates: true)

view = cardView
preferredContentSize = cardView.bounds.size

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM