【发布时间】:2015-12-17 09:59:19
【问题描述】:
我添加了以下代码以使以编程方式添加的视图居中:
let horizontalConstraint = NSLayoutConstraint(item: newView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)
它不起作用。视图未居中。它还在左边。
编辑:
override func viewDidLoad() {
super.viewDidLoad()
newView = LineChartView(frame: CGRectMake(0, 0, 50, 50))
newView?.delegate = self
newView?.drawBordersEnabled = true
newView?.noDataText = "No Data"
newView?.noDataTextDescription = "No Data"
newView?.borderColor = UIColor.blackColor()
self.view.addSubview(newView!)
【问题讨论】:
-
你能在
newView初始化的地方显示你的代码吗? -
尝试添加
newView?.setTranslatesAutoresizingMaskIntoConstraints(false) -
@hannad "'LineChartView' 类型的值没有成员 'setTranslatesAutoresizingMaskIntoConstraints'"
-
newView?.translatesAutoresizingMaskIntoConstraints = false -
还有你在哪里设置
newView的约束?
标签: ios swift autolayout uikit