如何在 swift 5 中显示模态屏幕?
How can I show a modal screen in swift5?
我目前正在从事 iOS 项目。
我的问题是,当我看到新屏幕时,我看不到旧屏幕,只看到新屏幕。
我想要模态屏幕。我错过了什么?
移动模态屏幕
func callAlert(_ text: String!) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
myAlert.modalPresentationStyle = .overCurrentContext
myAlert.modalTransitionStyle = .crossDissolve
myAlert.text = text
self.present(myAlert, animated: false, completion: nil)
}
modalScreenController
class ModalViewController : UIViewController {
var text: String?
@IBOutlet weak var modalCustomAlert: UITextView!
@IBOutlet weak var okButton: UIButton!
@IBAction func okPress(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text
}
...
modalStoryBoard
您需要在 viewcontroller 的视图中设置带有 alpha 分量的背景颜色。
class ModalViewController : UIViewController {
var text: String?
@IBOutlet weak var modalCustomAlert: UITextView!
@IBOutlet weak var okButton: UIButton!
@IBAction func okPress(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) //Your desired color and alpha component
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text
}
我目前正在从事 iOS 项目。
我的问题是,当我看到新屏幕时,我看不到旧屏幕,只看到新屏幕。
我想要模态屏幕。我错过了什么?
移动模态屏幕
func callAlert(_ text: String!) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
myAlert.modalPresentationStyle = .overCurrentContext
myAlert.modalTransitionStyle = .crossDissolve
myAlert.text = text
self.present(myAlert, animated: false, completion: nil)
}
modalScreenController
class ModalViewController : UIViewController {
var text: String?
@IBOutlet weak var modalCustomAlert: UITextView!
@IBOutlet weak var okButton: UIButton!
@IBAction func okPress(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text
}
...
modalStoryBoard
您需要在 viewcontroller 的视图中设置带有 alpha 分量的背景颜色。
class ModalViewController : UIViewController {
var text: String?
@IBOutlet weak var modalCustomAlert: UITextView!
@IBOutlet weak var okButton: UIButton!
@IBAction func okPress(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) //Your desired color and alpha component
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text
}