1
我想在名为containerView的UIView中添加一个UIButton。 containerView显示正常,但UIButton根本没有显示出来。这是我的代码:以编程方式在子视图内部创建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)
}
任何帮助?谢谢!
你可以测试你的按钮的x和y原点为0,看看会发生什么? –
你的容器视图是225宽,70高,你把你的按钮放在一个x值,这可能是在视图外,而y(200)肯定是。 – vacawama
尝试将x和y设置为宽度和高度的一半,以查看放入的坐标是否无效。这是通过self.view.frame.size.width/2 – Alex