0

I added the following code to center a programmatically added view:

let horizontalConstraint = NSLayoutConstraint(item: newView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)

It doesn't work. The view is not centered. It is on the left still.

EDIT:

    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!)
7
  • Can you show your code where newView is initialised please ? Commented Dec 17, 2015 at 10:02
  • 1
    Try to add newView?.setTranslatesAutoresizingMaskIntoConstraints(false) Commented Dec 17, 2015 at 10:09
  • @hannad "Value of type 'LineChartView' has no member 'setTranslatesAutoresizingMaskIntoConstraints'" Commented Dec 17, 2015 at 10:13
  • 1
    newView?.translatesAutoresizingMaskIntoConstraints = false Commented Dec 17, 2015 at 10:16
  • Also where abouts are you setting up your constraints for newView? Commented Dec 17, 2015 at 10:17

1 Answer 1

1

You need to pick a side my friend, If you are using auto layout, don't initialise your objects with a frame. Try something like this...

var newView:LineChartView!

override func viewDidLoad() {
    super.viewDidLoad()

    newView = LineChartView()
    newView.translatesAutoresizingMaskIntoConstraints = false
    newView.delegate = self
    newView.drawBordersEnabled = true
    newView.noDataText = "No Data"
    newView.noDataTextDescription = "No Data"
    newView.borderColor = UIColor.blackColor()
    self.view.addSubview(newView)

    let width = NSLayoutConstraint(item: newView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50)
    let height = NSLayoutConstraint(item: newView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50)
    newView.addConstraint(width)
    newView.addConstraint(height)
    let x = NSLayoutConstraint(item: newView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0)
    let y = NSLayoutConstraint(item: newView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0)
    self.view.addConstraint(x)
    self.view.addConstraint(y)

}

If your LineChartView object is a subclass of UIView then this should work, and you should have a 50x50 object in the middle of your superview.

If you are going to be doing constraints like this in code you should consider using Apples Visual Formatting Language.

Sign up to request clarification or add additional context in comments.

3 Comments

Personally I would still initialise LineChartView with a frame of CGRectZero, given initWithFrame: is one of the two designated initializers of UIView. Yes, init() for UIView calls initWithFrame:CGRectZero anyways, but correct initialization flow is more explicit.
When I remove the frame and just have double brackets, the view disappears.
Actually I replaced my code with yours exactly and now it works. Answer accepted. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.