大佬教程收集整理的这篇文章主要介绍了ios – “视图层次结构没有为约束准备”错误Swift 3,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
btn.settitle("mybtn",for: .normal) btn.settitleColor(UIColor.blue,for: .normal) btn.BACkgroundColor = UIColor.lightGray view.addSubview(btn) btn.translatesAutoresizingMaskIntoConsTraints = false let left = NSLayoutConsTraint(item: btn,attribute: .leftMargin,relatedBy: .equal,toItem: view,multiplier: 1.0,constant: 0) let right = NSLayoutConsTraint(item: btn,attribute: .rightMargin,constant: 0) let top = NSLayoutConsTraint(item: btn,attribute: .top,toItem: topLayoutGuide,attribute: .bottom,constant: 0) btn.addConsTraints([left,right,top])
或者甚至更好,正如Khalid所说,使用activate代替,在这种情况下,您无需担心视图层次结构中添加约束的位置:
let btn = UIButton(type: .system) btn.settitle("mybtn",for: .normal) btn.settitleColor(.blue,for: .normal) btn.BACkgroundColor = .lightGray view.addSubview(btn) btn.translatesAutoresizingMaskIntoConsTraints = false NSLayoutConsTraint.activate([ btn.leftAnchor.consTraint(equalTo: view.leftAnchor),btn.rightAnchor.consTraint(equalTo: view.rightAnchor),btn.topAnchor.consTraint(equalTo: topLayoutGuide.bottomAnchor) ])
以上是大佬教程为你收集整理的ios – “视图层次结构没有为约束准备”错误Swift 3全部内容,希望文章能够帮你解决ios – “视图层次结构没有为约束准备”错误Swift 3所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。