Assuming you have a UILabel property in your view named label ; you can set the center value to the center of your view in the layoutSubviews method:
override func layoutSubviews() { super.layoutSubviews() label.sizeToFit() label.center = self.convertPoint(self.center, fromView: self.superview) }
Please note that with Auto Layout this is much simpler, but you will not be able to change any frame or center as described above:
self.addConstraint(NSLayoutConstraint( item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0)) self.addConstraint(NSLayoutConstraint( item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
ozgur source share