swift / ios8:模仿动作表行为

时间:2015-01-16 11:57:15

标签: swift uiview ios8

由于非常不鼓励使用操作表进行叠加控制,我目前正试图用我自己的子视图模仿它,但不知何故包含的组件不会显示?

代码:

@IBAction func showActionSheet(sender: AnyObject) {

        println("SHOW STUFF")
        let newView:UIView = UIView()
        let myColor:UIColor = UIColor(red:255.0, green:255.0, blue:255.0, alpha:0.6)

        newView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
        newView.backgroundColor = myColor
        view.addSubview(newView)


        var pickerFrame: CGRect = CGRectMake(0, self.view.frame.height-(self.view.frame.height+self.view.frame.height-200), self.view.frame.width-20, self.view.frame.height/2)
        var picker: UIDatePicker = UIDatePicker(frame: pickerFrame);
        picker.backgroundColor = UIColor.whiteColor()
        view.addSubview(picker)

        var okButton = UIButton()
        okButton.titleLabel?.text = "Ok"
        okButton.backgroundColor = UIColor.whiteColor()
        okButton.frame = CGRectMake(0 , self.view.frame.width-160, self.view.frame.width, 80)

        var cancelButton = UIButton()
        cancelButton.titleLabel?.text = "Abbrechen"
        cancelButton.backgroundColor = UIColor.whiteColor()
        cancelButton.frame = CGRectMake(0 , self.view.frame.width-80, self.view.frame.width, 800)

        view.addSubview(okButton)
        view.addSubview(cancelButton)

        let secondView:UIView = UIView()

        secondView.frame = CGRectMake(0, 200, self.view.frame.width, self.view.frame.height)
        secondView.backgroundColor = UIColor.grayColor()
        view.addSubview(secondView)


    }

secondView仅用于测试目的 - 它将正常绘制,但不知何故我的控件不会

1 个答案:

答案 0 :(得分:0)

您没有将控件添加到newView放置在旧视图上的视图。按钮太大而且白色。试试这个代码。这将使它们可见。调整其高度以匹配视图的框架,然后将颜色设置回来。我只是改变了背景颜色以了解框架。同时拨打UIButton.setTitle(:forState:)设置按钮的标题。

@IBAction func showActionSheet(sender: AnyObject) {

    println("SHOW STUFF")
    let newView:UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
    let myColor:UIColor = UIColor(red:255.0, green:255.0, blue:0, alpha:0.6)

    newView.backgroundColor = myColor
    view.addSubview(newView)


    var pickerFrame: CGRect = CGRectMake(0, 0, self.view.frame.width-20, 200)
    var picker: UIDatePicker = UIDatePicker(frame: pickerFrame);
    picker.backgroundColor = UIColor.redColor()
    newView.addSubview(picker)

    var okButton = UIButton()
    okButton.setTitle("Ok", forState: UIControlState.Normal)
    okButton.setTitle("Ok", forState: UIControlState.Highlighted)
    okButton.backgroundColor = UIColor.greenColor()
    okButton.frame = CGRectMake(0 , self.view.frame.width-160, self.view.frame.width, 80)

    var cancelButton = UIButton()
    cancelButton.setTitle("Abbrechen", forState: UIControlState.Normal)
    cancelButton.setTitle("Abbrechen", forState: UIControlState.Highlighted)
    cancelButton.backgroundColor = UIColor.blueColor()
    cancelButton.frame = CGRectMake(0 , self.view.frame.width-80, self.view.frame.width, 200)

    newView.addSubview(okButton)
    newView.addSubview(cancelButton)

}