简体   繁体   中英

Horizontally Center UILabel + UIImageView inside UIView with Autolayout

First Problem:

I have UIView that contains UILabel (dynamic text) + UIImage . I want them to be centered inside the UIView . Just like these images:

在此输入图像描述

在此输入图像描述

The answer here is out of date and now we have stack view maybe it can make the task easier.

Second Problem:

I have three UIView's next to each other each one has UIImageView + UILabel that should be centered in their container (Problem 1). In the iPhone 4s who has a small width screen the text breaks the layout, like in this image :

在此输入图像描述

I use Xcode 7.0 | Target iOS8 and above | Swift 2

This is actually pretty easy to do in pure Autolayout/IB with no code. It ends up looking something like this: 在此输入图像描述

Here's the storyboard file with the view so you can see the constraints easily. It uses the same subview wrapper technique, but the trick to getting the labels to shrink/cut off as needed is adding >= 0 constraints to both sides of the wrapper view, keeping it from expanding past the borders of the parent view, which in turn keeps the label from growing.

https://www.dropbox.com/s/ml67zdjwugf8jco/SO_Solution-20151013.storyboard?dl=0

Clean contains and add again. I'm having this problem in Xcode 7.0, but it was apparently fixed in Xcode 7.0.1.

  1. Use Button (instead using ImageView & Label) and set appropriate image & title.
  2. set Image & Title Insets
  3. Add To View and Assign leading space & bottom space of superview constraint to first button
  4. Assign trailing space & bottom space of superview constraint to third button
  5. Assign Left Horizontal space (with first button) & Right Horizontal space (with third button) to second button.
  6. Add equal Width & fixed Height constraint with all three buttons.

also, you can add width constraint and make it with dynamic width.

In sort, you can complete it with button.

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