Centering a label in a UIView

前端 未结 2 1445
[愿得一人]
[愿得一人] 2021-02-15 18:04

What\'s the best way to center a label in a UIView? If you do something such as

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.fr         


        
2条回答
  •  花落未央
    2021-02-15 18:58

    To position any child horizontally centered in a parent you would calculate its position like so;

    childX = (parentWidth - childWidth) / 2
    

    (This also applies to height).

提交回复
热议问题