How to center a subview of UIView

前端 未结 14 1949
無奈伤痛
無奈伤痛 2020-11-30 16:47

I have a UIView inside a UIViewm and I want the inner UIView to be always centered inside the outer one, without it having to resize t

14条回答
  •  孤城傲影
    2020-11-30 17:28

    I would use:

    child.center = CGPointMake(parent.bounds.height / 2, parent.bounds.width / 2)
    

    This is simple, short, and sweet. If you use @Hejazi's answer above and parent.center is set to anything other than (0,0) your subview will not be centered!

提交回复
热议问题