以编程方式在子视图中创建UIButton不起作用

时间:2017-01-02 16:35:20

标签: ios swift uiview uibutton

我正在尝试在名为containerView的UIView中添加UIButton。 containerView正常显示,但UIButton根本没有显示。这是我的代码:

  let containerView = UIView()
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    self.containerView.frame =  CGRect(x: self.view.frame.size.width - 100, y: 200, width: 225, height: 70)
    self.containerView.backgroundColor = UIColor.gray
    self.containerView.layer.cornerRadius = 20
    self.containerView.clipsToBounds = true
    self.view.addSubview(self.containerView)

    let button1: UIButton = UIButton()
    button1.frame = CGRect(x: self.view.frame.size.width - 70, y: 200, width: 35, height: 35)
    button1.clipsToBounds = true
    button1.setTitle("Tesing Button", for: .normal)
    self.containerView.addSubview(button1)
   }

有任何帮助吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您的按钮框架位置不正确。您已将y的值设置为200,这超出了容器视图的大小。