UIAlertController: addSubview

泄露秘密 提交于 2019-12-02 03:32:54

问题


I want to add a subview to my alertcontroller. But why do the buttons go on the top ? How do I fix the problem?

    let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.alert)

    let somethingAction = UIAlertAction(title: "Something", style: .default, handler: {(alert: UIAlertAction!) in print("something")})

    let cancelAction = UIAlertAction(title: "Annuler", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

    alert.addAction(somethingAction)
    alert.addAction(cancelAction)

    let customView = UIView()
    customView.backgroundColor = .green
    customView.translatesAutoresizingMaskIntoConstraints = false
    customView.widthAnchor.constraint(equalToConstant: 128).isActive = true
    customView.heightAnchor.constraint(equalToConstant: 128).isActive = true

    alert.view.addSubview(customView)

    customView.centerXAnchor.constraint(equalTo: alert.view.centerXAnchor).isActive = true
    customView.topAnchor.constraint(equalTo: alert.view.topAnchor).isActive = true
    customView.bottomAnchor.constraint(equalTo: alert.view.bottomAnchor, constant: -32).isActive = true
    self.present(alert, animated: true, completion:{})


回答1:


UIAlertController is a pretty closed system. Its designed to be the system standard alert. You should not be adding subviews to it.

I would create an custom UIViewController that can act as an alert. You can use the custom UIViewController transition to make appear the same way UIAlertController does.

There are also many GitHub projects that provide custom alert styles that you might like. Such as this one: https://github.com/DominikButz/DYAlertController



来源:https://stackoverflow.com/questions/43002236/uialertcontroller-addsubview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!