How to center a label horizontally for all iOS devices in Swift

十年热恋 提交于 2019-12-05 04:37:43

for your label try to add constraints like:

Add top, width and height constraints as shown in below screen shot and make sure that constraints to margins is uncheck.

And set horizontal center constraints.

  1. Add Vertical top spacing for the particular imageview
  2. Add center horizontal at in same imageview
  3. Then adjust the leading and trailing as same as drag and drop way. it just easy.

just use this code :

yourLabel.translatesAutoresizingMaskIntoConstraints = YES;
CGPoint centerPoint = yourLabel.center;
centerPoint.x = yourImageView.center.x;
yourLabel.center = centerPoint;
Kishore Kumar

Click the label

  1. Don't set any heights

  2. Now set top space to the container view

  3. Centre horizontal to container view

  4. Now set the height (or) bottom space to the smiley

Add this all constraints you will get what you expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!