简体   繁体   English

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

[英]Creating a UIContainerView() programmatically and changing its height

I'm creating a container view programmatically. 我正在以编程方式创建容器视图。 However, i'm not able to change its height. 但是,我无法更改其高度。 I'm setting it programmatically but without any effect. 我正在以编程方式设置它,但没有任何效果。

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)
}

I created the view controller with identifier "Storyboard2" in the storyboard. 我在情节提要中创建了标识符为“ Storyboard2”的视图控制器。 And i've set its height to 70 there too. 我也将其高度设置为70。 But without any luck. 但是没有运气。 Any Help? 有帮助吗? Thanks 谢谢

You have not set the clipsToBounds on containerView , and the default value of that property is false . 您还没有设置clipsToBoundscontainerView ,并且该属性的默认值是false

Add this line just under you are setting the containerView 's frame: 在设置containerView的框架下方添加以下行:

containerView.clipsToBounds = true

Also, as some reading material, i would like to present to you this discussion about the clipsToBounds property. 另外,作为一些阅读材料,我想向您介绍有关clipsToBounds属性的讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM