Search code examples
iosiphonexcodeuiviewuilabel

Vertically center UILabel in parent UIView


How do I vertically (y-axis) center a UILabel inside its parent UIView without changing the horizontal position of the UILabel?

The following is not it (it changes the x-axis):

[self.label setCenter:CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2)]

Again, I am looking to center along the y-axis.


Solution

  • CGPoint center = self.label.center;
    center.y = view.frame.size.height / 2;
    [self.label setCenter:center];
    

    Swift Answer..

    var center : CGPoint = self.titleLbl.center
            center.y = self.frame.size.height / 2
            self.titleLbl.center = center