【问题标题】:Add image to ActionSheet and put it to right将图像添加到 ActionSheet 并将其放在右侧
【发布时间】:2019-06-12 08:13:53
【问题描述】:

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

第三方库不受欢迎。

谢谢!

Screenshot - AppStore

【问题讨论】:

    标签: swift uialertcontroller uiactionsheet swift5


    【解决方案1】:

    更新到 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
        }
    }
    

    您可以在视图控制器中使用此自定义类

    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)
    

    【讨论】:

    • 谢谢,这很好,但我想弄清楚如何使单元格高度与取消按钮相同
    【解决方案2】:

    这有点棘手,但可以实现; 首先不要使用UIActionSheet 它自iOS8以来已被弃用 使用UIAlertController 和preferredStyle = .actionSheet

    然后手动放置图像:

    let imageView = UIImageView(image: UIImage(named: "yourImage"))
    imageView.frame = CGRect(x: alertController.view.frame.width-25-24, y: 18, width: 24, height: 24)
    alertController.view.addSubview(imageView)
    

    使用alertController.view.frame.width-25-24 放置您的图像

    注意:这会将图像置于您的首要警报操作中。如果您想将图像放入多个动作中,您需要使用 y 坐标(例如)

    let imageViewAction2 = UIImageView(image: UIImage(named: "yourSecondImage"))
    alertController.view.addSubview(imageViewAction2)
    imageViewAction2.frame = CGRect(x: alertController.view.frame.width-25-24, y: 75, width: 24, height: 24)
    

    【讨论】:

      【解决方案3】:

      使用标准的UIAlertController API,这是不可能的。

      发布您自己的警报,或重新考虑您对第 3 方库的厌恶。

      来自文档:

      重要

      UIAlertController 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

      请注意,其他答案虽然看起来有效,但违反了此限制。如果您确实关注了他们,请注意他们可能随时停止工作,您的应用甚至可能被 Apple 拒绝。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2020-01-19
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多