gpt4 book ai didi

objective-c - 如何在 IOS 8 中设置 UIAlertController 的高度和宽度

转载 作者:行者123 更新时间:2023-12-03 18:34:49 26 4
gpt4 key购买 nike

我有一个带有 TextFile 的 UIAlertController。问题是默认的 UIAlertController 尺寸非常小。无法正确看到其文字。

所以,我想增加 UIAlertController 的高度和宽度。换句话说,我想创建一个自定义的 UIAlertController。有什么方法可以做到这一点?

最佳答案

您可以通过约束来控制 UIAlertController。

假设我们希望此警报更广泛:

Alert with big content

如果您查看 View 层次结构,您会发现 UIKit 将 UIAlertController 宽度限制为 270:

View hierarchy

检查这个 View 子项,你可以发现它的第一个子项也有这个约束,优先级为998。

知道我们可以在呈现警报 Controller 之前更新此约束。像这样的事情:

    let alert = UIAlertController(title: "Some gorgeous very big big big title with many words", message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a augue eget magna maximus posuere. Donec pellentesque lacus ut tellus mollis, eget congue nulla dapibus. Sed pharetra porta lorem, ac faucibus risus scelerisque vitae. Aenean lacinia lobortis quam quis finibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed odio nisl, pretium a turpis in, pulvinar bibendum augue. Proin id ligula elementum, pulvinar lorem et, suscipit turpis. Duis in tortor arcu. Donec in dapibus ex.\n\nDuis sit amet lacus nec mauris blandit dignissim. Sed efficitur vestibulum sapien ut condimentum. Donec a lorem sit amet augue imperdiet dictum sed eu sapien. Donec in congue quam, vitae luctus augue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus felis ipsum, malesuada eu dictum non, imperdiet ut urna. Vivamus tempus ante sit amet quam interdum feugiat. Ut at nulla nibh.", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Hide this", style: .default, handler: nil))
// Filtering width constraints of alert base view width
let widthConstraints = alert.view.constraints.filter({ return $0.firstAttribute == .width })
alert.view.removeConstraints(widthConstraints)
// Here you can enter any width that you want
let newWidth = UIScreen.main.bounds.width * 0.90
// Adding constraint for alert base view
let widthConstraint = NSLayoutConstraint(item: alert.view,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: newWidth)
alert.view.addConstraint(widthConstraint)
let firstContainer = alert.view.subviews[0]
// Finding first child width constraint
let constraint = firstContainer.constraints.filter({ return $0.firstAttribute == .width && $0.secondItem == nil })
firstContainer.removeConstraints(constraint)
// And replacing with new constraint equal to alert.view width constraint that we setup earlier
alert.view.addConstraint(NSLayoutConstraint(item: firstContainer,
attribute: .width,
relatedBy: .equal,
toItem: alert.view,
attribute: .width,
multiplier: 1.0,
constant: 0))
// Same for the second child with width constraint with 998 priority
let innerBackground = firstContainer.subviews[0]
let innerConstraints = innerBackground.constraints.filter({ return $0.firstAttribute == .width && $0.secondItem == nil })
innerBackground.removeConstraints(innerConstraints)
firstContainer.addConstraint(NSLayoutConstraint(item: innerBackground,
attribute: .width,
relatedBy: .equal,
toItem: firstContainer,
attribute: .width,
multiplier: 1.0,
constant: 0))

present(alert, animated: true, completion: nil)

现在您的提醒将占据屏幕的 90%:

90% wide screen alert

现在我只找到这个解决方案。可能有更优雅的变体和安全的解决方案,但我想你明白了。

关于objective-c - 如何在 IOS 8 中设置 UIAlertController 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com