<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arthur Avagyan on Medium]]></title>
        <description><![CDATA[Stories by Arthur Avagyan on Medium]]></description>
        <link>https://medium.com/@arthur.avagyan?source=rss-d6007ab8bdb3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*p4pa3kaBkJqTCInhtS3Zrw@2x.jpeg</url>
            <title>Stories by Arthur Avagyan on Medium</title>
            <link>https://medium.com/@arthur.avagyan?source=rss-d6007ab8bdb3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 04:04:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arthur.avagyan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Something You Didn’t Know About UITextField]]></title>
            <link>https://medium.com/@arthur.avagyan/something-you-didnt-know-about-uitextfield-1c3dbddf650c?source=rss-d6007ab8bdb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c3dbddf650c</guid>
            <category><![CDATA[issues]]></category>
            <category><![CDATA[bugs]]></category>
            <category><![CDATA[uitextfield]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[uikit]]></category>
            <dc:creator><![CDATA[Arthur Avagyan]]></dc:creator>
            <pubDate>Thu, 22 Feb 2024 15:06:56 GMT</pubDate>
            <atom:updated>2024-02-22T15:06:56.419Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*76yP-s8lY93sARaaP9LX1A.png" /></figure><p>At first glance, you might think there’s nothing new to know about UITextField. Well, I want you to think about one of its features: content type. To utilize this, we have the textContentType property of UITextField, and I hope every one of us is familiar with it.</p><pre>UITextField().textContentType = .name</pre><p>textContentType can be one of many types, and probably you have also used some of them. Me too, and not just once…</p><p>Recently, I started a new project, and like many applications across the internet, this one also had login and registration pages. Starting a new project is always interesting and sometimes challenging, but you don’t expect challenges to appear in unexpected places :)</p><p>While creating the registration page, I noticed something strange: an unexpected behavior of a text field.</p><p>That text field had a textContentType of familyName. You might say, <em>&quot;There&#39;s nothing strange about it; it should suggest the user&#39;s last name.&quot;.</em> But what if I tell you there was a password text field below it, with a textContentType of password?</p><p>Well, it’s not a common case to have a password just after the user’s last name, but why not? Can’t you ask for the user’s phone number, email, first name, last name, and then the password? <strong>Apple says NO!</strong> I was getting username suggestion on last name text field, regardless of the textContentType.</p><p>I researched a lot on this, and I didn’t find any information about what may or may not be the order of UITextFields.</p><p>Let’s start a new project to recreate this situation and try to find what causes the issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kdxxIenl3hk4nrmxo-tuhQ.png" /></figure><p>The same results can be achieved by using only Storyboards, but for our convenience, I will reproduce everything programmatically.<br>Let’s move directly to the ViewController and create several UITextFields .<br>We’ll create text fields for phone number, email, first name, last name, password, and repeat password.</p><pre> private func setUpViews() {<br>  phoneTextField = UITextField()<br>  phoneTextField.frame = .init(x: 16, y: 100, width: 350, height: 40)<br>  phoneTextField.placeholder = &quot;Phone&quot;<br>  phoneTextField.borderStyle = .roundedRect<br>  phoneTextField.textContentType = .telephoneNumber<br>  phoneTextField.keyboardType = .phonePad<br>  view.addSubview(phoneTextField)<br>  <br>  emailTextField = UITextField()<br>  emailTextField.frame = .init(x: 16, y: 150, width: 350, height: 40)<br>  emailTextField.placeholder = &quot;Email&quot;<br>  emailTextField.borderStyle = .roundedRect<br>  emailTextField.textContentType = .emailAddress<br>  emailTextField.keyboardType = .emailAddress<br>  view.addSubview(emailTextField)<br>  <br>  firstNameTextField = UITextField()<br>  firstNameTextField.frame = .init(x: 16, y: 200, width: 350, height: 40)<br>  firstNameTextField.placeholder = &quot;First Name&quot;<br>  firstNameTextField.borderStyle = .roundedRect<br>  firstNameTextField.textContentType = .givenName<br>  firstNameTextField.keyboardType = .default<br>  view.addSubview(firstNameTextField)<br>  <br>  lastNameTextField = UITextField()<br>  lastNameTextField.frame = .init(x: 16, y: 250, width: 350, height: 40)<br>  lastNameTextField.placeholder = &quot;Last Name&quot;<br>  lastNameTextField.borderStyle = .roundedRect<br>  lastNameTextField.textContentType = .familyName<br>  lastNameTextField.keyboardType = .default<br>  view.addSubview(lastNameTextField)<br>  <br>  passwordTextField = UITextField()<br>  passwordTextField.frame = .init(x: 16, y: 300, width: 350, height: 40)<br>  passwordTextField.placeholder = &quot;Password&quot;<br>  passwordTextField.borderStyle = .roundedRect<br>  passwordTextField.textContentType = .password<br>  passwordTextField.isSecureTextEntry = true<br>  passwordTextField.keyboardType = .default<br>  view.addSubview(passwordTextField)<br>  <br>  repeatPasswordTextField = UITextField()<br>  repeatPasswordTextField.frame = .init(x: 16, y: 350, width: 350, height: 40)<br>  repeatPasswordTextField.placeholder = &quot;Repeat Password&quot;<br>  repeatPasswordTextField.borderStyle = .roundedRect<br>  repeatPasswordTextField.textContentType = .password<br>  repeatPasswordTextField.isSecureTextEntry = true<br>  repeatPasswordTextField.keyboardType = .default<br>  view.addSubview(repeatPasswordTextField)<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8obA71f2jB_PVJxJWl0vlA.png" /></figure><p>As we see, all the text fields suggest correct information, except last name text field. For some reason, it shows username suggestion.</p><p>Let’s proceed by simplifying our code to Minimal Reproducible Example (MRE). In our case we’ll focus on the last name and password text fields.</p><pre> private func setUpViews() {<br>  lastNameTextField = UITextField()<br>  lastNameTextField.frame = .init(x: 16, y: 250, width: 350, height: 40)<br>  lastNameTextField.placeholder = &quot;Last Name&quot;<br>  lastNameTextField.borderStyle = .roundedRect<br>  lastNameTextField.textContentType = .familyName<br>  lastNameTextField.keyboardType = .default<br>  view.addSubview(lastNameTextField)<br>  <br>  passwordTextField = UITextField()<br>  passwordTextField.frame = .init(x: 16, y: 300, width: 350, height: 40)<br>  passwordTextField.placeholder = &quot;Password&quot;<br>  passwordTextField.borderStyle = .roundedRect<br>  passwordTextField.textContentType = .password<br>  passwordTextField.isSecureTextEntry = true<br>  passwordTextField.keyboardType = .default<br>  view.addSubview(passwordTextField)<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q7HGZ0u7uLeYGsykns2iIw.png" /></figure><p>In this case we see a similar issue: a password button leading to saved passwords.</p><p>Let’s make sure that the last name is suggested correctly when we have only one text field with textContentType set to .familyName.</p><pre> private func setUpViews() {<br>  lastNameTextField = UITextField()<br>  lastNameTextField.frame = .init(x: 16, y: 250, width: 350, height: 40)<br>  lastNameTextField.placeholder = &quot;Last Name&quot;<br>  lastNameTextField.borderStyle = .roundedRect<br>  lastNameTextField.textContentType = .familyName<br>  lastNameTextField.keyboardType = .default<br>  view.addSubview(lastNameTextField)<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4DXFV61gMG6xecn3BijQHA.png" /></figure><p>Now we see the last name being suggested as expected. This observation leads us to a theory that a text field with textContentType set to .password affects the functionality of the last name text field.</p><p>Let’s try to understand whether there are any textContentTypes that interact with the .password type as expected. Running ahead, I have already tried and found out that only when using three text fields with corresponding types .username , .password and another .password we get correct suggestion for the .username text field. For other types, we get <em>username</em> suggested.</p><pre> private func setUpViews() {<br>  usernameTextField = UITextField()<br>  usernameTextField.frame = .init(x: 16, y: 250, width: 350, height: 40)<br>  usernameTextField.placeholder = &quot;Username&quot;<br>  usernameTextField.borderStyle = .roundedRect<br>  usernameTextField.textContentType = .username<br>  usernameTextField.keyboardType = .default<br>  view.addSubview(usernameTextField)<br>  <br>  passwordTextField = UITextField()<br>  passwordTextField.frame = .init(x: 16, y: 300, width: 350, height: 40)<br>  passwordTextField.placeholder = &quot;Password&quot;<br>  passwordTextField.borderStyle = .roundedRect<br>  passwordTextField.textContentType = .password<br>  passwordTextField.isSecureTextEntry = true<br>  passwordTextField.keyboardType = .default<br>  view.addSubview(passwordTextField)<br>  <br>  repeatPasswordTextField = UITextField()<br>  repeatPasswordTextField.frame = .init(x: 16, y: 350, width: 350, height: 40)<br>  repeatPasswordTextField.placeholder = &quot;Repeat Password&quot;<br>  repeatPasswordTextField.borderStyle = .roundedRect<br>  repeatPasswordTextField.textContentType = .password<br>  repeatPasswordTextField.isSecureTextEntry = true<br>  repeatPasswordTextField.keyboardType = .default<br>  view.addSubview(repeatPasswordTextField)<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oJ1qBQq2bDozOPLIYOEXCQ.png" /></figure><p>Based on this we can kinda feel what’s going on here… Let’s move on…</p><p>With all these cases considered and taking into account that there’s no other factor affecting to this behavior apart from the x and y positions of the text fields. Let’s have an experiment by switching the order of just the password and last name text fields.</p><pre> private func setUpViews() {<br>  passwordTextField = UITextField()<br>  passwordTextField.frame = .init(x: 16, y: 300, width: 350, height: 40)<br>  passwordTextField.placeholder = &quot;Password&quot;<br>  passwordTextField.borderStyle = .roundedRect<br>  passwordTextField.textContentType = .password<br>  passwordTextField.isSecureTextEntry = true<br>  passwordTextField.keyboardType = .default<br>  view.addSubview(passwordTextField)<br>  <br>  lastNameTextField = UITextField()<br>  lastNameTextField.frame = .init(x: 16, y: 350, width: 350, height: 40)<br>  lastNameTextField.placeholder = &quot;Last Name&quot;<br>  lastNameTextField.borderStyle = .roundedRect<br>  lastNameTextField.textContentType = .familyName<br>  lastNameTextField.keyboardType = .default<br>  view.addSubview(lastNameTextField)<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UhvspO5b2gk9E3xfX3jp9Q.png" /></figure><p>Surprisingly, the x and y positions do have effect on this odd behavior. For us, this means that text fields of .password and .familyName types can coexist and function correctly on the same page, the only catch seems to be their positions. It’s also worth mentioning that the order of adding them as subviews does not matter.</p><p>Considering this, let’s have some more experiments to test our theory. We’ll position the last name and email text fields side by side above the password text field and manipulate their x and y positions. In this case, I am using .email for better understanding what suggestion we get, but similar results can be achieved with .username too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N9_KLnoWS_gCYUeLZdAxyQ.png" /><figcaption>Two screenshots on the left represent the same case, while two on the right represent another scenario. In both cases, both text fields have the same Y position and vary by X positions.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uBcXdETJ8Eh8UURn0Jbxqg.png" /><figcaption>In the left pair of screenshots, the last name text field has a Y value of 251. In the right pair of screenshots, the last name text field has a Y value of 249.</figcaption></figure><p>I have included only the screenshots, but you can find all the corresponding code in the <a href="https://github.com/ArthurAvagyan/Mastering-Text-Fields">GitHub repository</a>.</p><p>Based on the results of this experiment we can clearly see that both x and y position matter. Consequently, we can state that the username(email) text field is preferred to be positioned above the password text field to ensure correct functionality of suggestions.</p><p>Considering all these factors, I’ve determined the following optimal order for the text fields:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*quiQzwd824P25poCcR869Q.png" /><figcaption>First name, last name, phone number, email(username), password, repeat password.</figcaption></figure><p>In my case, it’s acceptable to use .email instead of .username type. And if you need, you can use for both of them. Just make sure that the text field of .email type stands before the text field of .username type, and just after those, there should be two text fields of .password type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zPG13ME8049IxOc2RLX2Ow.png" /></figure><h3>Conclusion</h3><p>We discovered that the order of the text fields is crucial. I have tried to find relevant documentation, none exists.</p><p>I consider this to be a bug in UIKit. Personally, I believe that there should either be strict rules and appropriate documentation regarding this behavior, or developers should have the flexibility to set the order of text fields as desired and get correct suggestions over the keyboard based on the specified type.</p><p>You can find the source code in <a href="https://github.com/ArthurAvagyan/Mastering-Text-Fields"><strong>this GitHub repository</strong></a><strong>.</strong></p><p>Please feel free to share your experiences, make suggestions, and correct me if I’m mistaken. I would be delighted to receive your feedback.</p><p>Thanks for your attention. Happy coding! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c3dbddf650c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>