Customised UIActionSheet

2020-07-11 09:49发布

How can I customise UIActionSheet to look like this :

enter image description here

Also, if I customise UIActionSheet, will there be a chance of app getting rejected ?

2条回答
Ridiculous、
2楼-- · 2020-07-11 10:30

In order to achieve this you can create your own custom control by subclassing a UIView and then add a UITableView into that View. Then you can create rows with custom cell having a label and UISwitch, and having a footer view to create reset and save in the footer of the UITableview. After that make that custom view transparent background. then pass an array of options to that view to create rows in the that custom control.

查看更多
三岁会撩人
3楼-- · 2020-07-11 10:41

If you really want to do something like that you could give this a go. I can't guarantee it'll be approved by Apple and honestly, it's just not recommended from a UI and Apple HIG perspective.

Keep in mind UIActionSheet has been deprecated and it's recommended to use UIAlertController with a preferredStyle of .ActionSheet so that's what this example is going to use.

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

Demo App

查看更多
登录 后发表回答