How to center a subview of UIView

前端 未结 14 1936
無奈伤痛
無奈伤痛 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:19

    Objective-C

    yourSubView.center = CGPointMake(yourView.frame.size.width  / 2, 
                                     yourView.frame.size.height / 2);
    

    Swift

    yourSubView.center = CGPoint(x: yourView.frame.size.width  / 2,
                                 y: yourView.frame.size.height / 2)
    

提交回复
热议问题