{"id":33278,"date":"2021-07-30T15:13:01","date_gmt":"2021-07-30T19:13:01","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=33278"},"modified":"2021-07-30T15:13:01","modified_gmt":"2021-07-30T19:13:01","slug":"implementing-a-focusable-text-field-in-swiftui","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/07\/30\/implementing-a-focusable-text-field-in-swiftui\/","title":{"rendered":"Implementing a Focusable Text Field in SwiftUI"},"content":{"rendered":"<p><a href=\"https:\/\/serialcoder.dev\/text-tutorials\/macos-tutorials\/macos-programming-implementing-a-focusable-text-field-in-swiftui\/\">Gabriel Theodoropoulos<\/a> (via <a href=\"https:\/\/iosdevweekly.com\/issues\/516\">Dave Verwer<\/a>):<\/p>\n<blockquote cite=\"https:\/\/serialcoder.dev\/text-tutorials\/macos-tutorials\/macos-programming-implementing-a-focusable-text-field-in-swiftui\/\"><p>What I mentioned above is definitely good news, but it regards evolvements that are going to be working in macOS Monterey and above. That&rsquo;s nice, but what about backward compatibility? What if we wanted to make apps that support macOS versions prior to Monterey, such as Big Sur or Catalina?<\/p>\n<p>The answer to these questions is what this post is all about. In the following parts, I will take you step by step through the implementation of a custom text field capable of:<\/p>\n<ol><li>Getting the focus automatically when a view appears; that means that the text field will be ready to start typing into without clicking on it first.<\/li><li>Moving the focus on subsequent text fields by pressing the Tab key.<\/li><\/ol><\/blockquote>\n\n<p>I still find it surprising&mdash;or maybe not, since it was designed for a device with neither a mouse nor a keyboard&mdash;that it took three releases for SwiftUI to get the equivalent of <a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nswindow\/1419440-firstresponder\">firstResponder<\/a>, one of the most basic Cocoa concepts. Maybe in a few more years it will support setting the <a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nsview\/1483465-nextkeyview\">nextKeyView<\/a>.<\/p>\n\n<p><a href=\"https:\/\/twitter.com\/schwa\/status\/1418445553982078979\">Jonathan Wight<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/schwa\/status\/1418445553982078979\"><p>&lt; 400 lines of SwiftUI<\/p>\n<p>That said I can&rsquo;t figure out how to layout a resizable username\/password login dialog without hardcoding widths or breaking causality.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/cocoaphony\/status\/1418553112957231109\">Rob Napier<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/cocoaphony\/status\/1418553112957231109\">\n<p>The paradox of SwiftUI.<\/p>\n<p>I find it utterly infuriating and incredibly promising.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/06\/08\/shortcuts-for-mac\/\">Shortcuts for Mac<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/11\/30\/what-is-not-so-great-about-swiftui\/\">What Is Not So Great About SwiftUI<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/08\/06\/emulating-equal-size-constraints-in-swiftui\/\">Emulating Equal-Size Constraints in SwiftUI<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Gabriel Theodoropoulos (via Dave Verwer): What I mentioned above is definitely good news, but it regards evolvements that are going to be working in macOS Monterey and above. That&rsquo;s nice, but what about backward compatibility? What if we wanted to make apps that support macOS versions prior to Monterey, such as Big Sur or Catalina? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-07-30T19:13:05Z","apple_news_api_id":"619af81f-9217-43ab-967f-1ea11277ff15","apple_news_api_modified_at":"2021-07-30T19:13:05Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AYZr4H5IXQ6uWfx6hEnf_FQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,30,1891,71,1812],"class_list":["post-33278","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-mac","tag-macos-11-0","tag-programming","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=33278"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33278\/revisions"}],"predecessor-version":[{"id":33279,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33278\/revisions\/33279"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=33278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=33278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=33278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}