回答
好吧,由於上面的討論,找到了一個解決方案。 基本思想是用 「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
}
}
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];
希望這有助於。
UIactionsheet已棄用 –
謝謝我將更新代碼 –
這不是問題,我問了關於文本對齊和圖像 –
是的,你需要添加一個UITableView到UIAlertController。
alertController.setValue([customTableGoesHere], forKey: "contentViewController")
我熟悉contentViewController,但是1:爲什麼UITableView而不是UIViewController的子類2:contentViewController只能幫助在UIAlertController內創建一個自定義內容,而不影響UIAlertAction。我可以創建自己的UIViewController並將其呈現在當前上下文中,但我希望影響UIAlertAction本身,如KVCing圖像... –
1.-我發現UITableView更容易添加多個單元格,我不知道KVCing的圖像是什麼意思,因爲我想要的作品。 –
KVC =關鍵值編碼,例如https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8,但我詢問對齊。也許KVCing contentViewController與包含圖像和文字的視圖控制器將解決它 –
- 1. 獲取UIAlertController動作片寬度
- 2. jQuery的片格動畫(窗簾風格)
- 3. 動作條的風格
- 4. UIAlertcontroller作爲Swift中的一個動作
- 5. Google+風格視圖/片段動畫?
- 6. 圖片風格的Drupal
- 7. 風格ActionSheet的UIAlertController顯示如下狀態欄
- 8. Rails中的非REST風格的動作
- 9. Pinterest風格的圖片網格
- 10. jQuery Mobile和Rails REST風格的動作
- 11. 改變動作條風格的Android
- 12. 如何風格的動作欄在styles.xml
- 13. Android - 自定義風格的動作欄
- 14. 動態添加的風格不工作
- 15. SelectableItemBackground不動作條的風格
- 16. Ruby on Rails ApplicationController REST風格的動作
- 17. MVC,改變動作鏈接的風格
- 18. 風格菜單項 - 圖片
- 19. 風格圖片元素
- 20. Android中的動作條像whatsapp
- 21. Whatsapp風格 - 鍵盤出現在工具欄下
- 22. 在Whatsapp發送圖片
- 23. 風格,格式化切片運算符
- 24. 股價片的iOS風格的按鈕
- 25. 迅速動態UIAlertController
- 26. UIAlertController操作不起作用?
- 27. Facebook風格的多圖片上傳
- 28. Facebook風格的多圖片上傳
- 29. 用css風格的代碼片段
- 30. 避免Python風格切片的缺陷
基本上你有什麼要求?你想要每個'actionSheetOption'圖標嗎? – Adeel
圖標和文字對齊 –
你想要什麼動作表或警報控制器? – Sanjukta