簡體   English   中英

按鈕操場迅速

[英]Button playground swift

我只是想添加一個創建視圖的按鈕動作。 有人可以幫我嗎?

import UIKit
import PlaygroundSupport

class ViewController : UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.black
        navigationController?.isNavigationBarHidden = true

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        button.backgroundColor = UIColor.white
        view.addSubview(button)

        class Button: ViewController {
            @objc func fbtn () {
                let newView = UIView()
                newView.backgroundColor = UIColor.yellow
                view.addSubview(newView)
            }
        }
        button.addTarget(button, action: #selector(Button.fbtn), for: .touchUpInside)
    }
}
PlaygroundPage.current.liveView = UINavigationController(rootViewController: ViewController())

嵌套的Button類似乎毫無意義。 只需將fbtn設置為ViewController類的函數即可。 並使self成為按鈕動作的目標。

class ViewController : UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .black
        navigationController?.isNavigationBarHidden = true

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        button.backgroundColor = .white
        view.addSubview(button)

        button.addTarget(self, action: #selector(fbtn), for: .touchUpInside)
    }

    @objc func fbtn() {
        let newView = UIView()
        newView.backgroundColor = .yellow
        view.addSubview(newView)
    }
}

另外,在fbtn函數內給newView一個有用的框架。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM