以编程方式创建 UIContainerView() 并更改其高度

Creating a UIContainerView() programmatically and changing its height

我正在以编程方式创建容器视图。但是,我无法改变它的高度。我正在以编程方式设置它,但没有任何效果。

let supportView: UIView = UIView()
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)
    print(self.containerView.frame.height)
    self.containerView.backgroundColor = UIColor.gray
    self.containerView.layer.cornerRadius = 20

    self.view.addSubview(self.containerView)

    let controller = storyboard!.instantiateViewController(withIdentifier: "Storyboard2")
    addChildViewController(controller)

    containerView.addSubview(controller.view)
    controller.didMove(toParentViewController: self)
}

我在故事板中创建了标识符为 "Storyboard2" 的视图控制器。我也将它的高度设置为 70。但没有任何运气。 有帮助吗?谢谢

您没有在 containerView 上设置 clipsToBounds,属性 的默认值为 false

在设置 containerView 的框架下方添加此行:

containerView.clipsToBounds = true

此外,作为一些阅读 material,我想向您介绍 this discussion 关于 clipsToBounds 属性。