将图像添加到 ActionSheet 并将其放在右侧 [英] Add image to ActionSheet and put it to right

查看:42
本文介绍了将图像添加到 ActionSheet 并将其放在右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to put the image to the right in an ActionSheet as seen on the AppStore. I know how to add an image and change the titleText style but I am unable to figure out how to change the positions of these two.

第三方库不受欢迎.

谢谢!

截图 - AppStore

推荐答案

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

Updated to swift 4.2 and added images. Go through below link for more info.

如何自定义 UIAlertControllerUITableView 或者在 Pages 应用程序中是否有像这个 UI 一样可用的默认控件?

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)

这篇关于将图像添加到 ActionSheet 并将其放在右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆