ios - Swift 如何获取 UIAlertController 标题高度

标签 ios swift swift3

目前我正在 swift 中开发 UIAlertController。我正在尝试向我的警报 Controller 添加事件指示器。我找到了以下解决方案,但是当标题大于一行时,微调器和标题就会重叠。为此,我想我需要知道标题标签的高度。

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.center = CGPoint(x: 130.5, y: 65.5)
spinner.startAnimating()
alert.view.addSubview(spinner)

请有人帮助我找到正确的解决方案。

最佳答案

据我所知,无法访问标题高度。很容易在警报 View 上添加自定义 UIView,然后在自定义 View 上添加标题标签、微调器、消息标签。

           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)

关于ios - Swift 如何获取 UIAlertController 标题高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45180904/

相关文章:

ios - 在 iOS 中禁用日文键盘的汉字自动完成 (henkan)?

java - 使用 SecRandomCopyBytes 生成范围内的随机数

ios - NSNotification 延迟

swift - 需要有关 swift 类型推断的帮助

objective-c - 将自定义 View 放入 UITableView

ios - NSMutableDictionary 中的更改未反射(reflect)在父 NSDictionary 中

swift - 为什么 BringSubviewToFront() 只适用于平移,而不适用于点击?

objective-c - Swift - 获取对具有相同名称但不同参数的函数的引用

ios - 获取 topVC 或 Visible VC 并检查特定 VC 是否在堆栈中

swift - 用 * 屏蔽名字和姓氏字符串