I created a stackView which incorporates two UILabels, then add this view to UIAlertController.view by perform addSubview(), for insteading of uialertcontroller’s title and message by particular types. now i can’t get Label’s or view’s peak to set buyer view’s peak and uilaertcontroler’s peak, now i set their peak with a set worth, however i need to understand robotically calculate the sum of peak of two labels and set the peak of subview and uialertcontroller’s peak. end it?
enter picture description right here
the next is my codes, the view’s peak is 300, and uialertcontroller’s peak is 400, make them calculated by tow of labels actual peak ?
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let selectAction = UIAlertAction(title: "Choose", fashion: .default) { (motion) in
print("choice")
}
let cancelAction = UIAlertAction(title: "Cancel", fashion: .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, fixed: 10).isActive = true
customView.rightAnchor.constraint(equalTo: alertController.view.rightAnchor, fixed: -10).isActive = true
customView.leftAnchor.constraint(equalTo: alertController.view.leftAnchor, fixed: 10).isActive = true
customView.heightAnchor.constraint(equalToConstant: **300**).isActive = true
alertController.view.translatesAutoresizingMaskIntoConstraints = false
alertController.view.heightAnchor.constraint(equalToConstant: **400**).isActive = true
customView.backgroundColor = .inexperienced
showAlert(actionSheetAlertController: alertController)
this perform to create a custom-made view
static func initCustomizedView(uiViewController: UIAlertController) -> UIView{
// 创建自定义视图
// 创建标题标签
let titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textual content = "Customized Title bbbbbbbbbbb"
//titleLabel.textual content = "Customized Title"
titleLabel.font = UIFont.boldSystemFont(ofSize: 40)
titleLabel.textColor = .brown
titleLabel.textAlignment = .middle
titleLabel.numberOfLines = 0 // 允许多行
// 创建消息标签
let messageLabel = UILabel()
messageLabel.translatesAutoresizingMaskIntoConstraints = false
messageLabel.textual content = "Customized Message bbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccc"
//messageLabel.textual content = "Customized Message"
messageLabel.font = UIFont.systemFont(ofSize: 30)
messageLabel.textColor = .yellow
messageLabel.textAlignment = .proper
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 = .inexperienced
return containerView