如何将自定义ViewController设置为swift中UIAlertController的内容
How to set custom ViewController as the content of UIAlertController in swift
我有自定义 ViewController 并想将其显示为警报。当我像下面那样做时,我的自定义 ViewController 不会填充 UIAlertController 的全部内容,并且会显示滚动条。如何做到这一点以及如何禁用滚动。
let alert : UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.setValue(myCustomViewController, forKey: "contentViewController")
self.present(alert, animated: true)
我认为你做不到。您可能会考虑以类似于警报控制器的方式呈现您的自定义视图控制器。一个技巧是使用自定义视图控制器的 modalPresentationStyle
。有了它,您可以制作一个看起来像 UIAlertController
的半透明背景视图控制器。例如:
myCustomViewController.modalPresentationStyle = .overCurrentContext
myCustomViewController.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
present(myCustomViewController, animated: true, completion: nil)
是的,你可以。
这是一个例子
var content_view_height:CGFloat = 200
var ctrl = MyViewController()
var alert = UIAlertController()
alert.setValue(ctrl, forKey: "contentViewController")
ctrl.preferredContentSize.height = content_view_height
alert.preferredContentSize.height = content_view_height
// you have to adjust the Alert size so it's bigger than your custom Content View Controller
var alert_height:CGFloat = 350
let custom_alert_height:NSLayoutConstraint = NSLayoutConstraint(
item: alert.view,
attribute: NSLayoutConstraint.Attribute.height,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: nil,
attribute: NSLayoutConstraint.Attribute.notAnAttribute,
multiplier: 1,
//constant: self.view.frame.height * 0.50 // e.g. half of current view
constant: alert_height // fixed
)
alert.view.addConstraint(custom_alert_height);
// then present it
我有自定义 ViewController 并想将其显示为警报。当我像下面那样做时,我的自定义 ViewController 不会填充 UIAlertController 的全部内容,并且会显示滚动条。如何做到这一点以及如何禁用滚动。
let alert : UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.setValue(myCustomViewController, forKey: "contentViewController")
self.present(alert, animated: true)
我认为你做不到。您可能会考虑以类似于警报控制器的方式呈现您的自定义视图控制器。一个技巧是使用自定义视图控制器的 modalPresentationStyle
。有了它,您可以制作一个看起来像 UIAlertController
的半透明背景视图控制器。例如:
myCustomViewController.modalPresentationStyle = .overCurrentContext
myCustomViewController.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
present(myCustomViewController, animated: true, completion: nil)
是的,你可以。
这是一个例子
var content_view_height:CGFloat = 200
var ctrl = MyViewController()
var alert = UIAlertController()
alert.setValue(ctrl, forKey: "contentViewController")
ctrl.preferredContentSize.height = content_view_height
alert.preferredContentSize.height = content_view_height
// you have to adjust the Alert size so it's bigger than your custom Content View Controller
var alert_height:CGFloat = 350
let custom_alert_height:NSLayoutConstraint = NSLayoutConstraint(
item: alert.view,
attribute: NSLayoutConstraint.Attribute.height,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: nil,
attribute: NSLayoutConstraint.Attribute.notAnAttribute,
multiplier: 1,
//constant: self.view.frame.height * 0.50 // e.g. half of current view
constant: alert_height // fixed
)
alert.view.addConstraint(custom_alert_height);
// then present it