iOS 11中Alert Dialog文字被截断如何解决?
修复iOS 11中UIAlertController(ActionSheet样式)文字截断问题
我来帮你解决iOS 11下UIAlertController ActionSheet文字截断的问题,这里有几个可行的修复方案,结合你的代码逐一说明:
1. 首先检查标题完整性
从你提供的代码片段来看,refreshCatalogs这个action的标题只写了"Od...",明显是不完整的。先把标题补全(比如对应波兰语完整标题"Odśwież katalogi"),不完整的字符串本身就会导致显示异常,这是最基础的排查点。
2. 处理iPad/大屏设备的Popover布局(关键修复)
在iOS 11中,ActionSheet在iPad上会以Popover形式展示,如果没有指定popoverPresentationController的源视图/区域,很容易出现布局挤压导致文字截断。你需要在present弹窗前添加以下代码:
if let popoverController = alert.popoverPresentationController { // 指定触发弹窗的按钮作为源视图 popoverController.sourceView = self.settingsButton popoverController.sourceRect = settingsButton.bounds // 允许任意方向的箭头 popoverController.permittedArrowDirections = .any }
这段代码能让Popover正确定位,避免布局空间不足导致文字被截断。
3. 调整Action标题字体(针对长标题场景)
如果你的Action标题确实较长,即使补全后还是截断,可以尝试通过KVC调整标题字体大小(苹果虽未公开此属性,但在iOS 11及后续版本中稳定可用):
let refreshCatalogs = UIAlertAction(title: "Odśwież katalogi", style: .default) { (action) in self.refreshCatalogs() } // 减小字体大小,让文字完整显示 refreshCatalogs.setValue(UIFont.systemFont(ofSize: 14), forKey: "titleTextFont") alert.addAction(refreshCatalogs)
修复后的完整代码示例
func settingsButtonPressed() { let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let closeAction = UIAlertAction(title: "Anuluj", style: .cancel) { (action) in // do nothing } alert.addAction(closeAction) let restorePurchases = UIAlertAction(title: "Przywróć zakupy", style: .default) { (action) in self.restorePurchases() } alert.addAction(restorePurchases) let refreshCatalogs = UIAlertAction(title: "Odśwież katalogi", style: .default) { (action) in self.refreshCatalogs() } // 可选:调整字体避免长标题截断 refreshCatalogs.setValue(UIFont.systemFont(ofSize: 14), forKey: "titleTextFont") alert.addAction(refreshCatalogs) // 处理iPad Popover布局 if let popoverController = alert.popoverPresentationController { popoverController.sourceView = self.settingsButton popoverController.sourceRect = settingsButton.bounds } self.present(alert, animated: true, completion: nil) }
内容的提问来源于stack exchange,提问作者Bartłomiej Janczak
相关产品推荐
相关产品推荐

