ли кто-нибудь знает, как создать UIAlertController как того, который WhatsApp сделал в следующем приложении (помимо создания пользовательского интерфейса)UIAlertController действия таблица стили WhatsApp
ответ
ну, в результате обсуждения выше, найдено решение. основная идея заключается в том, чтобы использовать "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 более легко добавить более одной ячейки, 2.- Я не знаю, что означает изображение KVCing, для чего я хотел работать. –
КВЦ = Значение ключа кодирования, см https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8 для примера, но я спрашиваю о выравнивании. может быть KVCing для contentViewController с представление-контроллер, который содержит изображение и текст будет решить –
В основном то, что ваше требование? Вам нужен значок с каждым 'actionSheetOption'? – Adeel
значок и выравнивание текста –
Что вы хотите, чтобы контроллер таблиц или предупреждений? – Sanjukta