UIAlertAction按钮文本左对齐
我想将UIAlertAction
文本对齐方式与Left对齐,并添加图标,如图所示。 我花了很多时间在谷歌上获得解决方案,但没有找到正确的答案。 警报标题的每个正文post都不是警报操作标题。
请建议我正确的方式快速 。
谢谢!!
您可以使用以下示例代码执行此操作。 简单易行。
斯威夫特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)
您可以使用以下示例代码执行此操作。 用图像替换图像名称
斯威夫特3
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let camera = UIAlertAction(title: "Camera", style: .default) { (action) in // Do something } let cameraImage = UIImage(named: "icon_camera") camera.setValue(cameraImage, forKey: "image") camera.setValue(0, forKey: "titleTextAlignment") actionSheet.addAction(camera)
正如在评论中已经说过的那样,UIAlertController不提供其视图的自定义。 但是,您可以构建自己的操作表视图或使用某些第三方,例如https://github.com/xmartlabs/XLActionController 。