ios - UIAlertAction 按钮文本左对齐

标签 ios swift uialertcontroller uialertaction

我想将 UIAlertAction 文本对齐方式向左对齐,并添加如图所示的图标。我花了很多时间在谷歌上寻找解决方案,但没有找到正确的答案。警报标题的每个正文帖子都不是警报操作标题。 enter image description here

请给我建议swift 的正确方法。

谢谢!!

最佳答案

您可以使用下面的示例代码来做到这一点。简单易行。

swift 4

let actionSheetAlertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheetAlertController.addAction(cancelActionButton)


let documentsActionButton = UIAlertAction(title: "Documents", style: .default, handler: nil)
actionSheetAlertController.addAction(documentsActionButton)
documentsActionButton.setValue(#imageLiteral(resourceName: "doccument"), forKey: "image")
documentsActionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")

let cameraActionButton = UIAlertAction(title: "Camera", style: .default, handler: nil)
actionSheetAlertController.addAction(cameraActionButton)
cameraActionButton.setValue(#imageLiteral(resourceName: "camera"), forKey: "image")
cameraActionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")

let galleryActionButton = UIAlertAction(title: "Gallery", style: .default, handler: nil)
actionSheetAlertController.addAction(galleryActionButton)
galleryActionButton.setValue(#imageLiteral(resourceName: "gallery"), forKey: "image")
galleryActionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")

actionSheetAlertController.view.tintColor = kTHEME_COLOR
self.present(actionSheetAlertController, animated: true, completion: nil)

关于ios - UIAlertAction 按钮文本左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47370823/

相关文章:

iOS:Swift – 无法弄清楚如何在 tableView 方法中使用 indexPath

ios - SKAction 不重启

ios - 检查 UIAlertController 是否出现在 XCTest 案例中

iOS - 解析 - PFQuery 不工作

ios - 重用单元格效果不佳 - TableView

ios - Twitter Fabric 登录 iOS 如何更改应用程序 key

ios - 在 Swift 中仅更改 AttributedText 的字体

swift - 使用 timeIntervalSince1970 时日期不相等

ios - 在 Swift 的 AppDelegate 中显示警报

ios - 如何为警报 View 创建类似于 iOS 默认警报的视觉效果?