sSubvIEw.center = vIEw.center
橙色属于紫色视图,相同的语法不能正确放置在中心.
你能看出我的错误在哪里吗?
Here is my code:import UIKitclass VIEwController: UIVIEwController {var sSubvIEw = UIVIEw()var sLabel = UILabel()overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. vIEw.backgroundcolor = UIcolor.lightGray}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated.}overrIDe func vIEwDIDLayoutSubvIEws() { createSimplestSubvIEw()}// First subvIEwfunc createSimplestSubvIEw() { sSubvIEw = UIVIEw(frame: CGRect(x: 0,y: 0,wIDth: vIEw.frame.wIDth * 0.9,height: vIEw.frame.height * 0.9)) sSubvIEw.backgroundcolor = UIcolor.purple // just for test,to make it visible sSubvIEw.center = vIEw.center // that s pretty easy! vIEw.addSubvIEw(sSubvIEw) createSimplestLabel() }// Second subvIEwfunc createSimplestLabel() { sLabel = UILabel(frame: CGRect(x: 0,wIDth: sSubvIEw.frame.wIDth * 0.3,height: sSubvIEw.frame.height * 0.2)) sLabel.backgroundcolor = UIcolor.orange // just for test,to make it visible sLabel.center = sSubvIEw.center sLabel.text = "This label has to be centered!" sLabel.textAlignment = .center sLabel.numberOflines = 0 sSubvIEw.addSubvIEw(sLabel)}
}
这是一个屏幕:
sLabel.center = CGPoint(x: sSubvIEw.frame.wIDth / 2,y: sSubvIEw.frame.height / 2)
您正在尝试在中心放置一个标签,但它是根据firstSbuvIEw框架计算的,该框架的原点不是0.0
总结以上是内存溢出为你收集整理的ios – 如何使用.center将子视图放在父视图的中心?全部内容,希望文章能够帮你解决ios – 如何使用.center将子视图放在父视图的中心?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)