How to center any subview in its parent UIview only horizontally, not vertically. I tried child.center = parent.center
but it place my child view in the center of the parent view both horizontally and vertically. How can i align it in center just horizontally. Thanks
Changing only the center like this may help
child.center = CGPointMake(CGRectGetMidX(parentView.bounds), child.center.y);
You can't modify the x
property of the UIViewController
's center
directly, but you can do the following:
CGPoint center = child.center;
center.x = parent.center.x;
child.center = center;
Try this,
subview.frame = CGRectMake(((parentView.frame.size.width/2)-(subview.frame.size.width/2)),subview.frame.origin.y,subview.frame.size.width,subview.frame.size.height);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With