0

I want add a UIView in its contentView center.

    @IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
    let view = UIView(frame: CGRectMake(0,0,100,100))
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    contentView.addSubview(view)
    view.center = contentView.center

}

But the result is

enter image description here

Did I forget something?

Update thanks @Wain's tip, Constrain work. According to https://stackoverflow.com/a/27624927/6006588

    override func viewDidLoad() {
    let view = UIView(frame: CGRectMake(0,0,100,100))
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(view)
    let widthConstraint = NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal,
                                             toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100)
    let heightConstraint = NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal,
                                              toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100)
     let xConstraint = NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0)
    let yConstraint = NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: contentView, attribute: .CenterY, multiplier: 1, constant: 0)
    NSLayoutConstraint.activateConstraints([heightConstraint,  widthConstraint,xConstraint, yConstraint])
}

I think I use constraint in StoryBoard to contentView (centerX , centerY , Equal Width , Equal Height to superView).
The contentView will have incorrect frame size in ViewDidLoad.
And when I set view.center = contentView.center it don't work.
I need use constraint programmatically to set view's position.
Thanks.

Community
  • 1
  • 1
Amulin
  • 15
  • 6
  • Yout code is working fine for me in 6S simulator but not 6S+. How ever i have implemented it by code. it is working fine for all simulators – Dimuth Aug 07 '16 at 06:28
  • Why aren't you using constraints? – Wain Aug 07 '16 at 07:26
  • @Wain . The reason is I need to add dynamic views whose receive from server in a scrollView. And I need add button in the each of view's center like Youtube play button. Do you have any good idea? – Amulin Aug 07 '16 at 09:02
  • yes, constraints... try it – Wain Aug 07 '16 at 09:04

2 Answers2

0

You can use this code

@IBOutlet weak var contentView: UIView!

Then add this to viewDidLoad

let view = UIView(frame: CGRectMake(0,0,100,100))
let contentView = UIView(frame: self.view.frame)
view.backgroundColor = UIColor.blueColor()
contentView.backgroundColor = UIColor.redColor()
view.center = contentView.center
contentView.addSubview(view)
self.view.addSubview(contentView)

This is the result

enter image description here

Dimuth
  • 713
  • 2
  • 11
  • 34
  • Thank you. It work. But Why reset contentView frame? I prefer set constraint in StoryBoard. Is constraint let view haven't correct size in viewDidLoad? – Amulin Aug 07 '16 at 09:31
-1

Try this -

let view = UIView(frame: CGRectMake((contentView.frame.size.width-100)/2,(contentView.frame.size.height-100)/2,100,100))
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    contentView.addSubview(view)
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63