簡體   English   中英

UIPopoverPresentationControllerDelegate 協議的 2 個 adaptivePresentationStyle 方法之間的區別

[英]Difference between 2 adaptivePresentationStyle methods of the UIPopoverPresentationControllerDelegate protocol

我正在使用彈出窗口視圖 controller,我不希望彈出窗口覆蓋全屏 (iOS 13)。 我試圖使用:

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
}

正在調用該方法,但顯示的彈出窗口始終是全屏,即使指定了較小的首選內容大小也是如此。 經過大量時間嘗試許多不同的事情后,我發現還有另一種方法有 2 個參數並使用它:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
}

使用此方法使彈出屏幕為指定大小。 任何人都知道為什么第二個會起作用而第一個不會。 我有幾個其他應用程序,我在其中使用第一個沒有問題,有什么用?!!

由於文檔:從 iOS 8.3 我們應該使用

自適應演示樣式(for:traitCollection:)

處理所有特征變化。 在哪里

UITraitCollection - iOS 界面環境,由水平和垂直尺寸類、顯示比例和用戶界面習慣等特征定義。

如果我們沒有在委托中實現這個方法,UIKit 會調用

自適應PresentationStyle(for:)

方法代替。

所以我猜在你的應用程序中你嘗試創建一個自適應界面並使用 UITraitCollection horizo​​ntalSizeClass、verticalSizeClass、displayScale 和 userInterfaceIdiom 屬性訪問特定的特征值。 這就是為什么你應該實現adaptivePresentationStyle(for:traitCollection:)

為了支持這個答案,我正在使用:

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

並在 iPad (iPadOS 16) 上正確獲取彈出窗口,但在 iPhone (iOS 16.2) 上全屏顯示

然后我改為:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return .none
}

並在兩個平台上獲得彈出窗口。

從文檔中不清楚為什么會這樣,因為它說如果你不在你的委托中實現adaptivePresentationStyle(for:traitCollection:) ,它只是默認在你的委托中調用adaptivePresentationStyle(for:)

暫無
暫無

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

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