swift 4.2实现操作表(ULAlertController
2019-01-08 本文已影响10人
yytester
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screen = UIScreen.main.bounds
let buttonActionSheet = UIButton(type: UIButton.ButtonType.system)
buttonActionSheet.setTitle("Test操作表", for: UIControl.State())
let buttonActionSheetWidth :CGFloat = 100
let buttonActionSheetHeight :CGFloat = 30
let buttonActionSheetTopView :CGFloat = 260
buttonActionSheet.frame = CGRect(x: (screen.size.width - buttonActionSheetWidth)/2, y: buttonActionSheetTopView, width: buttonActionSheetWidth, height: buttonActionSheetHeight)
buttonActionSheet.addTarget(self, action: #selector(testActionSheet(_:)), for: .touchUpInside)
self.view.addSubview(buttonActionSheet)
}
@objc func testActionSheet(_ sender:AnyObject){
let actionSheetController = UIAlertController()
let cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) {(UIAlertAction) -> Void in
print("Tap 取消 Button")
}
let destructiveAction = UIAlertAction(title: "破坏性按钮", style: UIAlertAction.Style.destructive) {(UIAlertAction) -> Void in
print("Tap 破坏性按钮 Button")
}
let otherAction = UIAlertAction(title: "新浪微博", style: UIAlertAction.Style.default) {(UIAlertAction) -> Void in
print("Tap 新浪微博 Button")
}
actionSheetController.addAction(cancelAction)
actionSheetController.addAction(destructiveAction)
actionSheetController.addAction(otherAction)
actionSheetController.popoverPresentationController?.sourceView = sender as? UIView
self.present(actionSheetController,animated: true,completion: nil)
}
}
image.png