2016-11-14 61 views

回答

2

好吧,由于上面的讨论,找到了一个解决方案。 基本思想是用 “contentViewController”

实施smaple

ViewController.swift

@IBAction func testActionSheetAction(_ sender: Any) { 

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

     let phoneAction = UIAlertAction(title: "", style: .default) { (_) in 
      print("phone action") 
     } 
     phoneAction.mode = .phone 
     sheet.addAction(phoneAction) 

     let homeAction = UIAlertAction(title: "", style: .default) { (_) in 
      print("home action") 
     } 
     homeAction.mode = .home 

     sheet.addAction(homeAction) 

     sheet.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil)) 

     self.present(sheet, animated: true, completion: nil) 

    } 

ActionSheetContentViewController.swift

import UIKit 

extension UIAlertAction{ 
    var mode : ActionSheetContentViewController.Mode?{ 
     set{ 
      let vc = ActionSheetContentViewController.viewController(with: newValue) 
      self.setValue(vc, forKey: "contentViewController") 
     } 
     get{ 
      if let vc = value(forKey: "contentViewController") as? ActionSheetContentViewController{ 
       return vc.mode 
      } 

      return nil 
     } 
    } 
} 

class ActionSheetContentViewController: UIViewController { 

    enum Mode{ 
     case home 
     case phone 

     var image : UIImage{ 
      get{ 
       switch self { 
       case .home: return #imageLiteral(resourceName: "icon_home") 
       case .phone: return #imageLiteral(resourceName: "icon_phone") 
       } 
      } 
     } 

     var title : String{ 
      get{ 
       switch self { 
       case .home: return NSLocalizedString("home", comment: "home") 
       case .phone: return NSLocalizedString("phone", comment: "phone") 
       } 
      } 
     } 
    } 

    @IBOutlet weak var label: UILabel! 
    @IBOutlet weak var imaegView: UIImageView! 

    var mode : Mode? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     label.text = mode?.title 
     imaegView.image = mode?.image 
    } 

    class func viewController(with mode : Mode?) -> UIViewController{ 

     let storyboard = UIStoryboard(name: "Main", bundle: .main) 
     let vc = storyboard.instantiateViewController(withIdentifier: "ActionSheetContentViewController") as! ActionSheetContentViewController 

     vc.mode = mode 

     return vc 

    } 
} 

​​

a screenshot

0
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { 
     //action when pressed button 
    }]; 

UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
     //action when pressed button 
    }]; 
[alertController addAction:cancelAction]; 
[alertController addAction:okAction]; 

希望这有助于。

+1

UIactionsheet已弃用 –

+0

谢谢我将更新代码 –

+0

这不是问题,我问了关于文本对齐和图像 –

0

是的,你需要添加一个UITableView到UIAlertController。

alertController.setValue([customTableGoesHere], forKey: "contentViewController") 
+0

我熟悉contentViewController,但是1:为什么UITableView而不是UIViewController的子类2:contentViewController只能帮助在UIAlertController内创建一个自定义内容,而不影响UIAlertAction。我可以创建自己的UIViewController并将其呈现在当前上下文中,但我希望影响UIAlertAction本身,如KVCing图像... –

+0

1.-我发现UITableView更容易添加多个单元格,我不知道KVCing的图像是什么意思,因为我想要的作品。 –

+0

KVC =关键值编码,例如https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8,但我询问对齐。也许KVCing contentViewController与包含图像和文字的视图控制器将解决它 –