title: Layout UITextField regardless of clear button X
body: <p>I need to horizontally center layout a <code>UITextField</code>, however, since the clear button is part of it, the (text entry portion of) text field doesn't look centered - the clear button isn't always visible but it is taken into account when laying out.</p>
  
  <p><a href="https://i.stack.imgur.com/eDLDi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eDLDi.png" alt="demo of the problem"></a></p>
  
  <p>In the image above, the <code>UITextField</code> is horizontally centered, however, without the clear button (X on the right) and with only search text, it doesn't look like centered.</p>
  
  <p>A way I can think of, is to subclass <code>UITextField</code> and provide <code>alignmentRectInsets</code>, for inset on the right I would use <code>clearButtonRect(forBounds:)</code> to get the size of clear button, whose <code>width</code> will be the right inset for <code>alignmentRectInsets</code>.</p>
  
  <p>Question:</p>
  
  <ol>
  <li><p>Apple's doc specifically mentions that one should not call <code>clearButtonRect(forBounds:)</code> directly, so I'm feeling a bit nervous for doing it; However, from the doc it feels like Apple's intention is to let people not changing the rect, and in my case I'm just getting its size, I guess it's fine?</p></li>
  <li><p>Is there any better way of achieving this? I know I can tweak <code>edgeInsets</code>, or give the text field a <code>leftView</code>, to make the text field looking centered; But they all need some hardcoded assumption for the size of the clear button.</p></li>
  </ol>
  
  <p>Thanks!</p>