回答
好吧,由于上面的讨论,找到了一个解决方案。 基本思想是用 “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. 动作条的风格
- 3. Google+风格视图/片段动画?
- 4. 图片风格的Drupal
- 5. 风格ActionSheet的UIAlertController显示如下状态栏
- 6. Pinterest风格的图片网格
- 7. MVC,改变动作链接的风格
- 8. 改变动作条风格的Android
- 9. 风格图片元素
- 10. Android中的动作条像whatsapp
- 11. 在Whatsapp发送图片
- 12. 用css风格的代码片段
- 13. Facebook风格的多图片上传
- 14. Facebook风格的多图片上传
- 15. 如何在android中做iphone风格的幻灯片动画?
- 16. 迅速动态UIAlertController
- 17. 风格的作用0x00000010
- 18. UIAlertController操作不起作用?
- 19. 的iOS UIAlertController中断动画
- 20. 风景和肖像照片的一种CSS风格
- 21. 禅代码 - 写作风格 - 图片src变化
- 22. 如何用凌乱的风格/混乱风格建立人照片库thromnail
- 23. 编码风格 - 图片链接描述
- 24. 如何风格背景图片CSS
- 25. 风格嵌入式Instagram照片横向
- 26. 水印图片下(9gag.com风格)
- 27. Google Currents风格幻灯片放映
- 28. 添加图片在消息框中(UIAlertController)
- 29. 如何制作ValidatorCalloutExtender风格?
- 30. 为datagrid.columns制作风格
基本上你有什么要求?你想要每个'actionSheetOption'图标吗? – Adeel
图标和文字对齐 –
你想要什么动作表或警报控制器? – Sanjukta