Weakman10122 Weakman10122 - 1 year ago 83
iOS Question

iOS layout: How to keep a button in the middle an image?

I have an image that needs a UITextField in the middle of it at all times. However, I can't get it do what I want.

I pinned the image with top, left and right constraint and gave it a height constraint. I put the UITextField in the middle of the image and tried various methods to keep it in the middle, but they all keep failing. Any tips are appreciated. Here is what the image currently looks like. First one iPhone 4 and second one is iPhone 6

enter image description here

enter image description here

Answer Source

What you need to do:

  • Embed the UIImageView and UITextField inside a UIView.
  • Set constraints of UIImageView leading, trailing, top, and bottom to UIView with constant of 0.
  • Set constraints of UITextField center horizontally with container view, and center vertically with container view.

Then it's up to you on where you want to position that UIView. This will definitely always position the UITextField to the center of UIImageView.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download