Updated to swift 4.2 and added images. Go through below link for more info.
How to customize UIAlertController with UITableView or is there any default control available like this UI in Pages app?
import Foundation
import UIKit
class CustomActionSheet: UIAlertController{
private var controller : UITableViewController
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
controller = UITableViewController(style: .plain)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
controller.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
controller.tableView.dataSource = self
controller.tableView.addObserver(self, forKeyPath: "contentSize", options: [.initial, .new], context: nil)
self.setValue(controller, forKey: "contentViewController")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
controller.tableView.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard keyPath == "contentSize" else {
return
}
controller.preferredContentSize = controller.tableView.contentSize
}
}
extension CustomActionSheet: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
switch(indexPath.row) {
case 0:
cell.textLabel?.text = "Share Link via iCloud"
cell.accessoryView = UIImageView(image:UIImage(named:"image1.png")!)
break
case 1:
cell.textLabel?.text = "Send a Copy"
cell.accessoryView = UIImageView(image:UIImage(named:"image2.png")!)
break
case 2:
cell.textLabel?.text = "Open in Another App"
cell.accessoryView = UIImageView(image:UIImage(named:"image3.png")!)
break
case 3:
cell.textLabel?.text = "Move to..."
cell.accessoryView = UIImageView(image:UIImage(named:"image4.png")!)
break
default:
fatalError()
}
return cell
}
}
You can use this custom class from your view controller
let controller = CustomActionSheet(title: nil, message: nil, preferredStyle: .actionSheet)
controller.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(controller, animated: true, completion: nil)