swift UIAlertAction按钮文本左对齐

1u4esq0p  于 2023-09-29  发布在  Swift
关注(0)|答案(6)|浏览(126)

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

请告诉我去机场的正确方法。
谢谢你!

ntjbwcob

ntjbwcob1#

正如在注解中已经说过的,UIAlertController不提供视图的自定义。但是,您可以构建自己的操作表视图或使用一些第三方,如https://github.com/xmartlabs/XLActionController

htzpubme

htzpubme2#

对于警报操作,可以使用以下代码,

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

let camera = UIAlertAction(title: "Camera", style: .default) { action in
            //add alert action
        }

camera.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
let icon = UIImage.(named: "your image file name")

camera.setValue(icon, forKey: "image")

actionSheet.addAction(camera)

present(actionSheet, animated: true)

使用此代码,您可以实现带有图像和左对齐警报操作文本的警报操作表。

fcipmucu

fcipmucu3#

你可以通过下面的示例代码来实现。简单易行。

  • Swift 5酒店**
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(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
    
let cameraActionButton = UIAlertAction(title: "Camera", style: .default, handler: nil)
actionSheetAlertController.addAction(cameraActionButton)
cameraActionButton.setValue(#imageLiteral(resourceName: "camera"), forKey: "image")
cameraActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
    
let galleryActionButton = UIAlertAction(title: "Gallery", style: .default, handler: nil)
actionSheetAlertController.addAction(galleryActionButton)
galleryActionButton.setValue(#imageLiteral(resourceName: "gallery"), forKey: "image")
galleryActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
    
actionSheetAlertController.view.tintColor = kTHEME_COLOR
self.present(actionSheetAlertController, animated: true, completion: nil)
4jb9z9bj

4jb9z9bj4#

kCAAlignmentLeft的swift 5.0的更新版本(如@Niraj Solanki here所回答的)是CATextLayerAlignmentMode.left,因此代码应如下所示:

Swift 5.0

documentsActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
az31mfrm

az31mfrm5#

你可以通过下面的示例代码来实现。将图像名称替换为您的图像

  • Swift 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)
fdbelqdn

fdbelqdn6#

这种情况下最快和最简单的实现:

extension UIAlertAction {
    func addImage(_ image: UIImage){
        setValue(image, forKey: "image")
        setValue(0, forKey: "titleTextAlignment")
    }
}

使用:

let editAction = UIAlertAction(title: "Edit", style: .default, handler: nil)
editAction.addImage(UIImage(systemName: "square.and.pencil")!)
alertSheet.addAction(editAction)

注意:CATextLayerAlignmentMode -不是您所需要的。因为右对齐不起作用!最好使用数字文字:

  • 0 -左侧
  • 单中心
  • 2 -右

为了通用,我会创建这个枚举:

enum AlertActionAlignment:Int {
    case left, center, right
}

...
setValue(align.rawValue, forKey: "titleTextAlignment")

相关问题