gpt4 book ai didi

swift - 将图像添加到 ActionSheet 并将其放在右侧

转载 作者:行者123 更新时间:2023-11-28 13:36:32 26 4
gpt4 key购买 nike

我想将图像放在 ActionSheet 的右侧,就像在 AppStore 上看到的那样。我知道如何添加图像并更改 titleText 样式,但我无法弄清楚如何更改这两者的位置。

第三方库不受欢迎。

屏幕截图 - AppStore

Screenshot

谢谢!

最佳答案

更新到 swift 4.2 并添加了图像。通过以下链接了解更多信息。

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
}
}

您可以在您的 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)

关于swift - 将图像添加到 ActionSheet 并将其放在右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com