-1

我有自定义 ViewController 并希望将其显示为警报。当我像下面那样做时,我的自定义 ViewController 不会填充 UIAlertController 的全部内容并显示滚动条。如何做到这一点以及如何禁用滚动。

let alert : UIAlertController =  UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.setValue(myCustomViewController, forKey: "contentViewController")
self.present(alert, animated: true) 
4

2 回答 2

2

我不认为你能做到这一点。您可能会考虑以类似于警报控制器的方式呈现您的自定义视图控制器。一个提示是使用modalPresentationStyle您的自定义视图控制器。有了它,您可以制作一个看起来像UIAlertController. 例如:

myCustomViewController.modalPresentationStyle = .overCurrentContext
myCustomViewController.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
present(myCustomViewController, animated: true, completion: nil)
于 2019-01-06T22:25:47.727 回答
0

是的你可以。

这是一个例子

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

于 2021-02-19T18:23:49.587 回答