一、需知
iOS13更新后擅腰,通過 present 方式呈現(xiàn)出來的畫面,默認呈現(xiàn)樣式變更為UIModalPresentationStyle.automatic
翁潘,表現(xiàn)為浮動效果趁冈,且可以通過下拉手勢將畫面dismiss
。
需求一:希望iOS13以上present
的畫面樣式跟以前一樣顯示為全屏效果
實現(xiàn)方式拜马,設置modalPresentationStyle
為fullScreen
渗勘。
1.在創(chuàng)建實例時設置,代碼如下:
let vc = ViewController()
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true, completion: nil)
2.或者在初始化方法中設置俩莽,代碼如下:
// 適用于純代碼構建的UIViewController
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
modalPresentationStyle = .fullScreen
}
// 適用于xib或storyboard構建的UIViewController
required init?(coder: NSCoder) {
super.init(coder: coder)
modalPresentationStyle = .fullScreen
}
需求二:希望iOS13以上保留浮動效果旺坠,禁用下拉手勢dismiss
畫面
實現(xiàn)方式:設置isModalInPresentation
屬性為true
。
代碼跟上面的實現(xiàn)方式一樣扮超。
需求三:監(jiān)測下拉手勢取刃,可以在用戶下拉時作出反應
此實現(xiàn)方式只適用于present
的畫面放在UINavigationController
里
實現(xiàn)方式:
1.設置代理
let vc = ViewController()
let navi = UINavigationController(rootViewController: vc)
navi.presentationController?.delegate = vc
present(vc, animated: true, completion: nil)
2.實現(xiàn)UIAdaptivePresentationControllerDelegate
的代理方法func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController)
,
此方法需同屬性isModalInPresentation
結合使用出刷,當isModalInPresentation = true
時璧疗,下拉試圖dismiss
畫面時,才會調用此方法巷蚪。
override func viewWillLayoutSubviews() {
// Ensure textView.text is up to date
textView.text = editedText
// If our model has unsaved changes, prevent pull to dismiss and enable the save button
let hasChanges = self.hasChanges
isModalInPresentation = hasChanges
saveButton.isEnabled = hasChanges
}
...
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
// The user pulled down with unsaved changes
// Clarify the user's intent by asking whether they intended to cancel or save
print("isModalInPresentation=\(self.isModalInPresentation)")
confirmCancel(showingSave: true)
}
上面的代碼來自官方 Simple Code病毡,請自行下載:
https://developer.apple.com/documentation/uikit/view_controllers/disabling_pulling_down_a_sheet