How to center a subview of UIView

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

    self.childView.center = CGPointMake(CGRectGetMidX(self.parentView.bounds),
                                        CGRectGetMidY(self.parentView.bounds));
    

    I like to use the CGRect options...

    SWIFT 3:

    self.childView.center = CGPoint(x: self.parentView.bounds.midX,
                                            y: self.parentView.bounds.midY);
    

提交回复
热议问题