简体   繁体   中英

UILabel's text not centered after changing interface orientation

There is a weird thing with label text after device is rotated, it's not centered as it should be. Basically the container view has 11 subviews (cells). The cell subviews (image and label) constrains are:

1) Label

在此处输入图片说明

2) Image

在此处输入图片说明

The cells constrains applied when they are added to the container view are:

  • Cell 1

    1) match top, left and right border (superview)

    2) height equal or bigger that 15.

  • Cell 2-10

    1) match left, right border (superview)

    2) pin to bottom of previous cell

    3) match height of previous cell

  • Cell 11

    1) match left, right, bottom border (superview)

    2) pin to bottom of previous cell

    3) match height of previous cell

The result is the following

  • Portrait

在此处输入图片说明

  • Landscape

在此处输入图片说明

This seems a bug to me, but not sure. Any suggestions are welcome.

I would put the label inside it's own UIView object, then use the Centre Horizontally/Vertically constraints to ensure the label is centred inside the view.

Pin the label's superview's top and bottom to the top and bottom of the coloured image, and give it an equal width constraint with the image. Add spacing between them and around it's edges and this should achieve what you're looking for!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM