ios - Swift 以编程方式向按钮添加显示操作

标签 ios iphone swift

如何以编程方式向按钮添加操作。我需要向 mapView 中的按钮添加显示操作。谢谢

let button = UIButton(type: UIButtonType.Custom) as UIButton

最佳答案

你可以使用下面的代码
`

let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn) 

行动

 @objc func buttonAction(sender: UIButton!) {
        let btnsendtag: UIButton = sender
        if btnsendtag.tag == 1 {

            dismiss(animated: true, completion: nil)
        }
    }

关于ios - Swift 以编程方式向按钮添加显示操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35550966/

相关文章:

ios - 等待嵌套的 AFNetworking 调用和循环完成

ios - 无法在 iOS 中通过 cocoa pod 安装 Realm

ios - 移动到新 View Controller 时隐藏导航栏

iphone - 如何为 Cydia 和越狱的 iPhone 开发应用程序

ios - iAd 横幅占用不必要的空间

ios - 处理多个 NSURL 连接的最佳方式

iphone - 淹没/显示选项卡栏

ios - Swift中如何在Key和String之间进行转换?

ios - 使用 ionic for ios 构建 html5 应用程序

swift - 如何在不像 Whatsapp 那样遮挡整个屏幕的情况下禁用 tableview?