Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone上的多个按钮-Swift_Swift_Uibutton - Fatal编程技术网

iPhone上的多个按钮-Swift

iPhone上的多个按钮-Swift,swift,uibutton,Swift,Uibutton,我试图在一个iPhone应用程序上设置多个按钮,只有当我第二次运行该功能时,按钮才会显示出来。它没有显示我配置它运行的两个按钮 class ViewController: UIViewController { let button = UIButton.buttonWithType(UIButtonType.System) as UIButton let screensize: CGRect = UIScreen.mainScreen().bounds overrid

我试图在一个iPhone应用程序上设置多个按钮,只有当我第二次运行该功能时,按钮才会显示出来。它没有显示我配置它运行的两个按钮

class ViewController: UIViewController {
    let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    let screensize: CGRect = UIScreen.mainScreen().bounds
    override func viewDidLoad() {

        super.viewDidLoad()
        println(screensize.size.height)
        blur()


        // add iphone 2 buttons
        let rect : CGRect = CGRectMake(100, 50, 200, 50)
        var rectObj = NSValue(CGRect: rect)
        showButton("Hello", rect: rectObj, redvalue: 1.0, greenvalue: 0.5, bluevalue: 0.5, alphavalue: 1.0)

        let rect1 : CGRect = CGRectMake(20, 50, 200, 50)
        var rectObj1 = NSValue(CGRect: rect)
        showButton("Good bye", rect: rectObj1, redvalue: 0.5, greenvalue: 0.5, bluevalue: 0.5, alphavalue: 1.0)
    }

    func showButton(title: String, rect: NSValue, redvalue : CGFloat, greenvalue: CGFloat, bluevalue: CGFloat, alphavalue : CGFloat)
    {

        var rectRestored : CGRect = rect.CGRectValue()
        button.frame = CGRectMake(rectRestored.origin.x, rectRestored.origin.y, rectRestored.width, rectRestored.height)
        button.backgroundColor = UIColor(red: redvalue , green: greenvalue, blue: bluevalue, alpha: alphavalue)
        button.setTitle(title, forState: UIControlState.Normal)
        self.view.addSubview(button)
    }

    func blur()
    {
        var blur = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
        blur.frame = self.view.frame
        self.view.addSubview(blur)
    }

addSubView是否覆盖它?如果是这样的话,我尝试了insersubview:index,但这似乎也不起作用

尝试在viewDidLoad中调用方法将取代viewDidLoad,因为在viewDidLoad中,您可能仍然没有主视图的适当框架。

不,这不起作用,我只是想有没有一种更快的方法以编程方式显示多个按钮。