How to center a UILabel on UIView

前端 未结 7 2013
日久生厌
日久生厌 2020-12-13 06:12

How can I center the UILabel on the UIView? I am using the following code

float width = weatherView.bounds.size.width; 
float height = weatherView.bounds.siz         


        
7条回答
  •  长情又很酷
    2020-12-13 06:44

    If the label is a subview of view:

    [self.label setCenter:CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2)]
    

    Don't use view.center unless the label and view have the same superview.

    If there is no immediate connection, then you'll have to transform view's center to the coordinate space of label's parent. Then you could use PengOne's answer with the transformed point.

提交回复
热议问题