Swift 2開發完全可定制和可擴展的Action sheet controller

jopen 9年前發布 | 7K 次閱讀 Apple Swift開發 XLActionController

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.baiduhome.net/lib/view/home/1450170328672

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!