Swift 2开发完全可定制和可扩展的Action sheet controller

jopen 9年前

XLActionController 是一个可扩展的库用于快速创建任何定制的action sheet controller。

Examples

The action sheet controllers shown above were entirely created using XLActionController and are included in the Examples. To run the Example project: clone XLActionController repository, open XLActionController workspace and run the Example project.

The code snippet below shows how to present the Tweetbot action sheet controller:

let actionController = TweetbotActionController()    actionController.addAction(Action("View Details", style: .Default, handler: { action in    // do something useful  }))  actionController.addAction(Action("View Retweets", style: .Default, handler: { action in    // do something useful  }))  actionController.addAction(Action("View in Favstar", style: .Default, handler: { action in    // do something useful  }))  actionController.addAction(Action("Translate", style: .Default, handler: { action in    // do something useful  }))    actionController.addSection(Section())  actionController.addAction(Action("Cancel", style: .Cancel, handler:nil))    presentViewController(actionController, animated: true, completion: nil)

As you may have noticed, the library usage looks pretty similar to UIAlertController.

Behind the scenes XLActionController uses a UICollectionView to display the action sheet.

Usage

First create a custom action sheet view controller by extending from theActionControllergeneric class. For details on how to create a custom action sheet controller look at the Extensibility section.

For instance, let's suppose we've already created 推terActionController.

// Instantiate custom action sheet controller  let actionSheet = 推terActionController()  // set up a header title  actionSheet.headerData = "Accounts"  // Add some actions, note that the first parameter of `Action` initializer is `ActionData`.  actionSheet.addAction(Action(ActionData(title: "Xmartlabs", subtitle: "@xmartlabs", image: UIImage(named: "tw-xmartlabs")!), style: .Default, handler: { action in     // do something useful  }))  actionSheet.addAction(Action(ActionData(title: "Miguel", subtitle: "@remer88", image: UIImage(named: "tw-remer")!), style: .Default, handler: { action in     // do something useful  }))  // present actionSheet like any other view controller  presentViewController(actionSheet, animated: true, completion: nil)

项目主页:http://www.open-open.com/lib/view/home/1450170328672