William Wang
William Wang

Reputation: 13

Is there a way to automatically set subview height of UIAlertController?

I created a stackView which includes two UILabels, then add this view to UIAlertController.view by function addSubview(), for insteading of uialertcontroller's title and message by special styles. now i cannot get Label's or view's height to set customer view's height and uilaertcontroler's height, now i set their height with a fixed value, but i want to realize automatically calculate the sum of height of two labels and set the height of subview and uialertcontroller's height. how to finish it?

enter image description here

the following is my codes, the view's height is 300, and uialertcontroller's height is 400, how to make them calculated by tow of labels real height ?

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let selectAction = UIAlertAction(title: "Select", style: .default) { (action) in
            print("selection")
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertController.addAction(selectAction)
        alertController.addAction(cancelAction)
        
        let customView = Popover.initCustomizedView(uiViewController: alertController)
       
            alertController.view.addSubview(customView)
            customView.translatesAutoresizingMaskIntoConstraints = false
            customView.topAnchor.constraint(equalTo: alertController.view.topAnchor, constant: 10).isActive = true
            customView.rightAnchor.constraint(equalTo: alertController.view.rightAnchor, constant: -10).isActive = true
            customView.leftAnchor.constraint(equalTo: alertController.view.leftAnchor, constant: 10).isActive = true
            customView.heightAnchor.constraint(equalToConstant: **300**).isActive = true

            alertController.view.translatesAutoresizingMaskIntoConstraints = false
            alertController.view.heightAnchor.constraint(equalToConstant: **400**).isActive = true

            customView.backgroundColor = .green

           
        showAlert(actionSheetAlertController: alertController)

this function to create a customized view

static func initCustomizedView(uiViewController: UIAlertController) -> UIView{
        // 创建自定义视图
        
        
        // 创建标题标签
        let titleLabel = UILabel()
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.text = "Custom Title bbbbbbbbbbb"
        //titleLabel.text = "Custom Title"
        titleLabel.font = UIFont.boldSystemFont(ofSize: 40)
        titleLabel.textColor = .brown
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0 // 允许多行
        
        
        // 创建消息标签
        let messageLabel = UILabel()
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.text = "Custom Message bbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccc"
        //messageLabel.text = "Custom Message"
        messageLabel.font = UIFont.systemFont(ofSize: 30)
        messageLabel.textColor = .yellow
        messageLabel.textAlignment = .right
        messageLabel.numberOfLines = 0 // 允许多行
        
        // 将标签添加到自定义视图
        let margin:CGFloat = 10.0
  
        let stackView = UIStackView(arrangedSubviews: [titleLabel, messageLabel])
        stackView.axis = .vertical
        stackView.spacing = 10 // Add spacing between labels
        stackView.alignment = .fill
        stackView.distribution = .equalSpacing

        let containerView = UIView()
        containerView.addSubview(stackView)
        
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            stackView.topAnchor.constraint(equalTo: containerView.topAnchor),
            stackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
        ])
        
        containerView.layoutIfNeeded()
        containerView.backgroundColor = .green
        
        return containerView

Upvotes: 0

Views: 32

Answers (0)

Related Questions