Swift 如何获取 UIAlertController 标题高度 [英] Swift how to get UIAlertController title height

查看:27
本文介绍了Swift 如何获取 UIAlertController 标题高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在快速开发 UIAlertController.我正在尝试向警报控制器添加活动指示器.我找到了以下解决方案,但是当标题大于一行时,微调器和标题重叠.为此,我想,我需要知道标题标签的高度.

Currently I'm working on UIAlertController in swift. I'm trying to add an activity indicator to my alert controller. I have found following solution, but when the title is bigger than one line, then spinner and title overlapped. For this I guess, I need to know the height of title label.

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

请有人帮我找到合适的解决方案.

Someone please help me with a proper solution.

推荐答案

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

As I know, it is not possible to access title height. It is easy to add a custom UIView on alert view, then add title label, spinner, message label on the custom 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)

这篇关于Swift 如何获取 UIAlertController 标题高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆