定制化的UIActionSheet

6
我该如何定制UIActionSheet,使其看起来像这样:

enter image description here

另外,如果我自定义了UIActionSheet,是否有被拒绝的风险?

2
只要您使用的是来自苹果的公共API,他们就不会拒绝您的应用程序,但这取决于这个答案会对您有多大帮助。 - iphonic
苹果不会拒绝你的应用程序。 - Anbu.Karthik
@gotnull:嗯,我对上面链接中提到的答案不满意 :) - Nitish
@Anbu.Karthik 你无法保证这一点。 - fulvio
2个回答

11

如果你真的想这样做,你可以试一试。我不能保证它会被苹果公司批准,而且从用户界面和苹果公司 HIG 规范的角度来看,这并不被推荐。

请记住,UIActionSheet 已被弃用,建议使用 preferredStyle.ActionSheetUIAlertController,所以这个示例将使用它。

import UIKit

class ViewController: UIViewController {

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let controller = SwiftDemoAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        controller.addAction(UIAlertAction(title: "Reset to default", style: .Destructive, handler: nil))
        controller.addAction(UIAlertAction(title: "Save", style: .Default, handler: nil))

        self.presentViewController(controller, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

class SwiftDemoAlertController: UIAlertController, UITableViewDataSource {

    private var controller : UITableViewController

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        controller = UITableViewController(style: .Plain)
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        controller.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        controller.tableView.dataSource = self
        controller.tableView.addObserver(self, forKeyPath: "contentSize", options: [.Initial, .New], context: nil)
        self.setValue(controller, forKey: "contentViewController")
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        guard keyPath == "contentSize" else {
            return
        }

        controller.preferredContentSize = controller.tableView.contentSize
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        controller.tableView.removeObserver(self, forKeyPath: "contentSize")
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")!

        switch(indexPath.row) {
        case 0:
            cell.textLabel?.text = "Upcoming activities"
            let switchView = UISwitch(frame: CGRectZero)
            cell.accessoryView = switchView
            switchView.setOn(true, animated: false)
            break
        case 1:
            cell.textLabel?.text = "Past activities"
            let switchView = UISwitch(frame: CGRectZero)
            cell.accessoryView = switchView
            switchView.setOn(false, animated: false)
            break
        case 2:
            cell.textLabel?.text = "Activities where I am admin"
            let switchView = UISwitch(frame: CGRectZero)
            cell.accessoryView = switchView
            switchView.setOn(true, animated: false)
            break
        case 3:
            cell.textLabel?.text = "Attending"
            let switchView = UISwitch(frame: CGRectZero)
            cell.accessoryView = switchView
            switchView.setOn(true, animated: false)
            break
        case 4:
            cell.textLabel?.text = "Declined"
            let switchView = UISwitch(frame: CGRectZero)
            cell.accessoryView = switchView
            switchView.setOn(true, animated: false)
            break
        case 5:
            cell.textLabel?.text = "Not responded"
            let switchView = UISwitch(frame: CGRectZero)
            cell.accessoryView = switchView
            switchView.setOn(true, animated: false)
            break
        default:
            fatalError()
        }

        return cell
    }
}

演示应用程序


谢谢你的解决方案,但我不能采用它,因为它可能会被拒绝。不过我可能会选择一个UITableView。 - Nitish
我可以用Objective-C实现这个吗? - Nitish

0
为了实现这一点,您可以通过子类化UIView创建自己的自定义控件,然后将一个UITableView添加到该View中。然后您可以创建带有标签和UISwitch的自定义单元格行,并在UITableview的页脚视图中创建重置和保存选项。之后,使该自定义视图具有透明背景。接着,将一个选项数组传递给该视图,以在自定义控件中创建行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接