目前,我就是这样向UIAlertController
的左侧添加图标的。
let chooseImage = UIImage(systemName: "photo")
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let chooseImageAction = UIAlertAction(title: "choose_image".localized, style: .default) { _ in
self.chooseImage()
}
chooseImageAction.setValue(chooseImage, forKey: "image")
alert.addAction(chooseImageAction)
这就是它的样子
但是,我也想在右边添加一个图标,如红色圆圈所示。
我想请问,是否可以做到呢?谢谢。
发布于 2022-05-01 09:27:54
我认为默认的操作是不可能的,但是您可以使用自定义操作,对于我的商品,我使用一个导航按钮来调用自定义操作表(您可以使用简单的按钮),在viewDidLoad中设置我的按钮并调用handleAS func来显示我的自定义操作表:
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Show ActionSheet", style: .plain, target: self, action: #selector(handleAS))
从上面的按钮调用的函数:
@objc fileprivate func handleAS() {
let controller = CustomActionSheet(title: nil, message: nil, preferredStyle: .actionSheet)
controller.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) // controller cancel Action
self.present(controller, animated: true, completion: nil)
}
现在创建您的CustomActionSheet类:
class CustomActionSheet: UIAlertController {
private var controller : UITableViewController
let cellId = "cellID"
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
controller = UITableViewController(style: .plain)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
controller.tableView.register(ActionSheetCell.self, forCellReuseIdentifier: cellId) // register your custom cell
controller.tableView.dataSource = self
controller.tableView.delegate = 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
}
}
CustomActionSheet对tableViewDelegate和数据资源的扩展
extension CustomActionSheet: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? ActionSheetCell else { return }
print("This is:", cell.loadingLabel.text ?? "")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4 // number of action in actionSheet
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId) as! ActionSheetCell
switch(indexPath.row) {
case 0:
cell.loadingLabel.text = "Choose image"
cell.myImageView.image = UIImage(systemName: "photo")?.withRenderingMode(.alwaysTemplate)
cell.myImageView2.image = UIImage(systemName: "clock")?.withRenderingMode(.alwaysTemplate)
break
case 1:
cell.loadingLabel.text = "Take a photo"
cell.myImageView.image = UIImage(systemName: "camera")?.withRenderingMode(.alwaysTemplate)
cell.myImageView2.image = UIImage(systemName: "car")?.withRenderingMode(.alwaysTemplate)
break
case 2:
cell.loadingLabel.text = "Drawing"
cell.myImageView.image = UIImage(systemName: "paintbrush.pointed")?.withRenderingMode(.alwaysTemplate)
cell.myImageView2.image = UIImage(systemName: "paintbrush")?.withRenderingMode(.alwaysTemplate)
break
case 3:
cell.loadingLabel.text = "Recording"
cell.myImageView.image = UIImage(systemName: "mic")?.withRenderingMode(.alwaysTemplate)
cell.myImageView2.image = UIImage(systemName: "cloud")?.withRenderingMode(.alwaysTemplate)
break
default:
fatalError()
}
cell.selectionStyle = .none // comment if do you want selection of row
return cell
}
}
创建自定义单元格:
class ActionSheetCell: UITableViewCell {
let loadingLabel: UILabel = {
let label = UILabel()
label.text = "Loading Results..."
label.textColor = #colorLiteral(red: 0, green: 0.4199056029, blue: 0.9801818728, alpha: 1)
label.textAlignment = .center
label.font = .systemFont(ofSize: 18, weight: .regular)
label.heightAnchor.constraint(equalToConstant: 55).isActive = true
return label
}()
let myView: UIView = {
let iv = UIView()
iv.backgroundColor = .clear
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFit
iv.widthAnchor.constraint(equalToConstant: 55).isActive = true
iv.clipsToBounds = true
return iv
}()
let myView2: UIView = {
let iv = UIView()
iv.backgroundColor = .clear
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFit
iv.widthAnchor.constraint(equalToConstant: 55).isActive = true
iv.clipsToBounds = true
return iv
}()
let myImageView: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(systemName: "car")?.withRenderingMode(.alwaysTemplate)
iv.tintColor = #colorLiteral(red: 0, green: 0.4199056029, blue: 0.9801818728, alpha: 1)
iv.contentMode = .scaleAspectFit
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()
let myImageView2: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(systemName: "clock")?.withRenderingMode(.alwaysTemplate)
iv.tintColor = #colorLiteral(red: 0, green: 0.4199056029, blue: 0.9801818728, alpha: 1)
iv.contentMode = .scaleAspectFill
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
myView.addSubview(myImageView)
myImageView.centerYAnchor.constraint(equalTo: myView.centerYAnchor).isActive = true
myImageView.centerXAnchor.constraint(equalTo: myView.centerXAnchor).isActive = true
myImageView.widthAnchor.constraint(equalToConstant: 24).isActive = true
myImageView.heightAnchor.constraint(equalToConstant: 24).isActive = true
myView2.addSubview(myImageView2)
myImageView2.centerYAnchor.constraint(equalTo: myView2.centerYAnchor).isActive = true
myImageView2.centerXAnchor.constraint(equalTo: myView2.centerXAnchor).isActive = true
myImageView2.widthAnchor.constraint(equalToConstant: 24).isActive = true
myImageView2.heightAnchor.constraint(equalToConstant: 24).isActive = true
let stackView = UIStackView(arrangedSubviews: [myView, loadingLabel, myView2])
stackView.distribution = .fill
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在didSelectRowAt中,您可以调用其他函数..。
其结果是:
https://stackoverflow.com/questions/72061724
复制相似问题