私が知っているように、タイトルの高さにアクセスすることはできません。アラートビューにカスタムUIViewを追加して、カスタムビューにタイトルラベル、スピナー、メッセージラベルを追加するのは簡単です。
let alert = UIAlertController(title: " ", message: " ", preferredStyle: UIAlertControllerStyle.alert)
let customViewWidth: CGFloat = 270
let viewRect = CGRect(x: 0, y: 0, width: customViewWidth, height: 150)
let customView = UIView(frame: viewRect)
customView.backgroundColor = UIColor.white
customView.layer.cornerRadius = 20.0
customView.clipsToBounds = true
var spinnerTopPadding: CGFloat = 17.0
if(!title.isEmpty) {
let titleRect = CGRect(x: 13.0, y: 17.0, width: customViewWidth - 26, height: 100)
let titleLabel = UILabel(frame: titleRect)
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 0
titleLabel.font = titleLabel.font(withSize: 17.0)
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.text = title
titleLabel.sizeToFit()
titleLabel.center = CGPoint(x: customViewWidth/2, y: titleLabel.frame.size.height/2 + 17.0)
customView.addSubview(titleLabel)
spinnerTopPadding = titleLabel.frame.size.height + 27
}
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.center = CGPoint(x: customViewWidth/2, y: spinnerTopPadding + spinner.frame.size.height/2)
spinner.startAnimating()
customView.addSubview(spinner)
var messageText = message.replacingOccurrences(of: "\n\n", with: "")
messageText = message.replacingOccurrences(of: "\n", with: "")
var spinnerBottomPadding: CGFloat = 17.0
if (!message.isEmpty) {
let messageRect = CGRect(x: 13.0, y: spinnerTopPadding + spinner.frame.size.height + 10.0, width: customViewWidth - 26, height: 100)
let messageLabel = UILabel(frame: messageRect)
messageLabel.textAlignment = .center
messageLabel.numberOfLines = 0
messageLabel.font = messageLabel.font(withSize: 14.0)
messageLabel.lineBreakMode = .byWordWrapping
messageLabel.text = messageText
messageLabel.sizeToFit()
messageLabel.center = CGPoint(x: customViewWidth/2, y: spinnerTopPadding + spinner.frame.size.height + messageLabel.frame.size.height/2 + 10)
customView.addSubview(messageLabel)
spinnerBottomPadding = messageLabel.frame.size.height + 27
}
customView.frame.size.height = spinnerTopPadding + spinner.frame.size.height + spinnerBottomPadding
alert.view.addSubview(customView)
let alertControllerHeight = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: customView.frame.size.height)
let alertControllerWidth = NSLayoutConstraint(item: alert.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: customViewWidth)
alert.view.addConstraint(alertControllerHeight)
alert.view.addConstraint(alertControllerWidth)
let alertContainer = alert.view.subviews.first!.subviews.first!
for container in alertContainer.subviews {
container.backgroundColor = UIColor.white
container.layer.cornerRadius = 20.0
}
self.present(alert, animated: true, completion: nil)