Why this happens
A subview’s center is expressed in its parent’s coordinate system. Setting it once may put the subview in the right place initially, but it does not automatically keep it there when the parent’s bounds change. That can happen during rotation or when a container is resized.
The layout method matters too. Auto Layout constraints can reposition a view after you set its frame or center manually. If you use autoresizing masks instead, flexible width or height changes the subview’s size; flexible margins allow it to move without stretching.
How to fix it
1. Center the subview with Auto Layout
Use center constraints when the rest of your interface uses Auto Layout. Add fixed width and height constraints so the subview stays the same size while its parent changes size.
import UIKit
let parent = UIView()
let child = UIView()
child.backgroundColor = .systemBlue
child.translatesAutoresizingMaskIntoConstraints = false
parent.addSubview(child)
NSLayoutConstraint.activate([
child.centerXAnchor.constraint(equalTo: parent.centerXAnchor),
child.centerYAnchor.constraint(equalTo: parent.centerYAnchor),
child.widthAnchor.constraint(equalToConstant: 120),
child.heightAnchor.constraint(equalToConstant: 80)
])
In your app, use your existing parent and child views instead of creating new ones. Make sure the child is added to the parent before activating the constraints. The parent also needs a size and position supplied by its own frame or constraints.
The two center constraints control position; the width and height constraints control size. Auto Layout recalculates the position when the parent’s layout changes. Remove conflicting constraints that also try to determine the child’s position or dimensions.
These constraints center the child in the whole parent. If you want the center of the parent’s safe area instead, constrain the child’s center anchors to parent.safeAreaLayoutGuide.centerXAnchor and parent.safeAreaLayoutGuide.centerYAnchor.
2. Update the center during manual layout
If you manage frames yourself, calculate the center from the parent’s bounds. The bounds rectangle is in the parent’s own coordinate system, which is the coordinate system expected by the child’s center property.
A custom parent view can update the child during layoutSubviews():
import UIKit
final class ContainerView: UIView {
private let child = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
setUpChild()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUpChild()
}
private func setUpChild() {
child.backgroundColor = .systemBlue
child.bounds.size = CGSize(width: 120, height: 80)
addSubview(child)
}
override func layoutSubviews() {
super.layoutSubviews()
child.center = CGPoint(x: bounds.midX, y: bounds.midY)
}
}
The child keeps its existing size because this code changes its center, not its bounds. UIKit can call layoutSubviews() again when the container needs to lay out its contents, so the position is recalculated rather than being fixed at the initial size.
Use this approach for a child whose frame you control manually. Don’t also add Auto Layout constraints that control the same child’s position: a later layout pass can replace the position you set.
A commonly suggested one-time calculation is:
child.center = CGPoint(
x: parent.frame.size.width / 2,
y: parent.frame.size.height / 2
)
That works in the usual case where the parent’s bounds origin is zero and its frame dimensions correspond to its bounds dimensions. Using parent.bounds.midX and parent.bounds.midY is safer because it uses the coordinate space the child actually needs. In either case, repeat the calculation when the parent’s layout changes if you are positioning manually.
3. Use flexible margins with an autoresizing mask
For a frame-based view hierarchy, an autoresizing mask can keep a subview centered as the parent resizes. Start by setting the child’s size and center, then make all four margins flexible:
import UIKit
let child = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 80))
parent.addSubview(child)
child.center = CGPoint(x: parent.bounds.midX, y: parent.bounds.midY)
child.autoresizingMask = [
.flexibleLeftMargin,
.flexibleRightMargin,
.flexibleTopMargin,
.flexibleBottomMargin
]
Here, parent is an existing UIView. Leave .flexibleWidth and .flexibleHeight out of the mask so the child does not stretch as its parent changes size. Flexible margins distribute the change in available space around the child; they don’t correct an incorrectly centered starting position, which is why the initial center matters.
In Interface Builder, the equivalent springs-and-struts setup uses flexible space on all four sides and a fixed width and height. If constraints are already controlling the child, use the Auto Layout solution instead of relying on the autoresizing mask.
4. Convert the parent’s center when working across coordinate spaces
Another approach is to convert the parent’s center from its superview’s coordinate system into the parent’s coordinate system:
if let superview = parent.superview {
child.center = parent.convert(parent.center, from: superview)
}
This illustrates the coordinate-space distinction: parent.center belongs to the parent’s superview, while child.center belongs to the parent. The conversion requires the parent to have a superview in this example.
For centering a direct child, the midpoint of parent.bounds is usually simpler and also works before the parent has been added to a superview. Like the one-time bounds calculation, the conversion must be repeated when layout changes unless another layout mechanism keeps the child in position.
How to prevent it
- Choose one layout approach for the child: constraints, autoresizing, or manual positioning.
- Use the parent’s
boundswhen calculating a direct child’s center. - Give an Auto Layout child enough size constraints, and remove conflicting position constraints.
- Test after resizing the container and rotating the device, not just at the initial size.
- Decide whether the child should be centered in the whole parent or its safe area.
FAQ
Why doesn’t setting the child’s center once keep it centered?
The value is a position at the time you assign it. If the parent later changes size, a manually positioned child needs an updated center or a layout mechanism that adjusts it.
Why does Auto Layout move my subview after I set its center?
Constraints determine the child’s layout during a layout pass. If they specify a different position, UIKit can update the frame and center to satisfy those constraints. Change the constraints rather than setting the center directly.
Can I center a subview without changing its width and height?
Yes. Use center constraints with width and height constraints, or set the center manually while leaving the child’s size unchanged. With an autoresizing mask, use flexible margins but not flexible width or height.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.