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

空扰寡人 提交于 2019-12-07 00:09:07

问题


I can't figure out how to make the label horizontally center in a image view, The label says "You've been here. What would you rate us, I want the What would you rate us to be under the "You've been here." What I attempted to do accomplish this was to add 2 lines to the labels and select the label plus the 3 buttons and clicked on "Add Missing Constrains". When I run the program this is the result.

How can I fix it so the text is entered on all devices?

Constrains


回答1:


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.




回答2:


  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.



回答3:


just use this code :

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



回答4:


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.



来源:https://stackoverflow.com/questions/34448619/how-to-center-a-label-horizontally-for-all-ios-devices-in-swift

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