我创建了带有图像视图的警报,我需要对该图像进行四舍五入,并且我需要提供背景颜色以警报完成按钮。请帮助我输入代码。
这是我的警报代码:
func showAlert(){
let alertController = UIAlertController(title:" Profile created \n Sccessfully ", message: "", preferredStyle: .alert)
let doneAlert = UIAlertAction(title: "DONE", style: .default, handler: { (action) -> Void in
self.navigationController?.popViewController(animated: true)
})
alertController.addAction(doneAlert)
image.layer.cornerRadius = image.frame.height/2
image.layer.borderWidth = 1
alertController.view.addSubview(image)
image.translatesAutoresizingMaskIntoConstraints = false
alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: alertController.view, attribute: .centerX, multiplier: 1, constant: 0))
alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: alertController.view, attribute: .top, multiplier: 1, constant: -20))
alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
self.present(alertController, animated: true, completion: nil)
}
我需要这样的警报
答案 0 :(得分:3)
从文档中:
重要
UIAlertController类旨在按原样使用,并且不支持子类化。此类的视图层次结构是私有的,不能修改。
您最好使用实现此功能的现有库,例如https://github.com/vikmeup/SCLAlertView-Swift,或从头开始实施您自己的警报。
答案 1 :(得分:0)
您可以使用以下方法。它调整了UIAlertController
func showConfiguredUIAlertControllerWithTitle(_ title:String, message:String, messageTextAlignment:NSTextAlignment, actionButtonTitle:String, actionViewBackgroundColor color:UIColor, alertTint:UIColor){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
//configuring the alert view sans action
let alertMainSubviews = alert.view.subviews.first?.subviews.first?.subviews.first?.subviews
let upperView = (alertMainSubviews?.first)! as UIView
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = messageTextAlignment
let attributedStringTitle = NSAttributedString(string: title, attributes: [NSAttributedString.Key.foregroundColor: UIColor.orange])
let attributedStringMessage = NSAttributedString(string: "\n" + message, attributes: [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.paragraphStyle: paragraphStyle])
alert.setValue(attributedStringTitle, forKey: "attributedTitle")
alert.setValue(attributedStringMessage, forKey: "attributedMessage")
let line = UIView(frame: CGRect(x: 0, y: 45, width: alert.view.frame.size.width, height: 1.0))
line.backgroundColor = .black
upperView.addSubview(line)
upperView.bringSubviewToFront(line)
//configuring the action view
let actionView = (alertMainSubviews?[1])! as UIView
actionView.backgroundColor = color
alert.view.tintColor = alertTint
//adding action to alert
let action = UIAlertAction(title: actionButtonTitle, style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
按以下方式调用它:
showConfiguredUIAlertControllerWithTitle("Hey There!", message: "I am your friendly neighbourhood Spiderman", messageTextAlignment: .left, actionButtonTitle: "OK", actionViewBackgroundColor: .orange, alertTint: .white)