{"id":76,"date":"2017-01-07T21:23:00","date_gmt":"2017-01-07T21:23:00","guid":{"rendered":"https:\/\/seimith.io\/?p=76"},"modified":"2024-07-20T00:38:47","modified_gmt":"2024-07-20T00:38:47","slug":"swift-keyboard-visibility","status":"publish","type":"post","link":"https:\/\/seimith.io\/pt\/2017\/01\/07\/swift-keyboard-visibility\/","title":{"rendered":"(Swift) Keyboard Visibility"},"content":{"rendered":"<p>This post is about doing things when your keyboard is visible or not. You might want to have some animation when your keyboard comes into view, or you might want an alert to pop up. Whatever it may be, here\u2019s a snippet for that.<\/p>\n\n\n\n<p>In the example below, I just have a&nbsp;<code>UITextView<\/code>&nbsp;and a&nbsp;<code>UIButton<\/code>&nbsp;set up so that you can see the keyboard appear and disappear.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/seimith\/seimith.github.io\/master\/_assets\/2017-01-07-assets\/keyboardLarge.gif\" alt=\"Keyboard toggling\" title=\"Keyboard toggling\"\/><\/figure>\n\n\n\n<p><strong>Tools:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Swift 3.0<\/li>\n\n\n\n<li>Xcode Version 8.0<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 1: Set up the UITextView<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let myCoolTextArea = UITextView()\nmyCoolTextArea.backgroundColor = UIColor.gray\nmyCoolTextArea.frame = CGRect(x: 0, y: 20, width: self.view.frame.width, height: 300)\nmyCoolTextArea.text = \"This is a textarea! :)\"\nself.view.addSubview(myCoolTextArea)<\/code><\/pre>\n\n\n\n<p><strong>Step 2: Set up a UIButton to dismiss your keyboard<\/strong>&nbsp;In this step I&nbsp;<code>add a target<\/code>&nbsp;to a function named&nbsp;<code>dismissMyKeyboard<\/code>&nbsp;to hide my keyboard.<\/p>\n\n\n\n<p>Check out my previous post on how to add a target&nbsp;<a href=\"http:\/\/seimith.github.io\/swift\/addtarget\/click%20handle\/snippet\/2016\/12\/26\/Swift-Add-Target-for-Click-Handle.html\">here<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let myButtonToDismissKeyboard = UIButton()\nmyButtonToDismissKeyboard.backgroundColor = UIColor.gray\nmyButtonToDismissKeyboard.frame = CGRect(x: 0, y: 350, width: self.view.frame.width, height: 30)\nmyButtonToDismissKeyboard.setTitle(\"Dimiss Button\", for: .normal)\nmyButtonToDismissKeyboard.addTarget(self, action: #selector(dismissMyKeyboard), for: .touchUpInside)\nself.view.addSubview(myButtonToDismissKeyboard)<\/code><\/pre>\n\n\n\n<p><strong>Step 3: Fuctions<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func keyboardWillAppear (_ notification: NSNotification) {\n    print(\"=== keyboardWillAppear\")\n}\n\nfunc keyboardWillDisappear (_ notification: NSNotification) {\n    print(\"=== keyboardWillDisappear\")\n}\n\nfunc dismissMyKeyboard () {\n    self.view.endEditing(true)\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 4: Add Observers<\/strong>&nbsp;This is similar to adding targets. In order for my functions&nbsp;<code>and<\/code>&nbsp;to be observed and then get called, I need to add the following to my&nbsp;<code>viewDidLoad<\/code>&nbsp;function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillAppear), name: NSNotification.Name.UIKeyboardWillShow, object: nil)\n\nNotificationCenter.default.addObserver(self, selector:#selector(keyboardWillDisappear), name: NSNotification.Name.UIKeyboardWillHide, object: nil)<\/code><\/pre>\n\n\n\n<p><strong>You can get the storyboard project is in&nbsp;<a href=\"https:\/\/github.com\/seimith\/SwiftKeyboardVisibility\">this repo<\/a>.<\/strong><\/p>","protected":false},"excerpt":{"rendered":"<p>This post is about doing things when your keyboard is visible or not. You might want to have some animation when your keyboard comes into view, or you might want an alert to pop up. Whatever it may be, here\u2019s a snippet for that. In the example below, I just have a&nbsp;UITextView&nbsp;and a&nbsp;UIButton&nbsp;set up so [&hellip;]<\/p>","protected":false},"author":1,"featured_media":288,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20],"class_list":["post-76","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-post","tag-code"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>(Swift) Keyboard Visibility - S(ei)mith.io<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/seimith.io\/pt\/2017\/01\/07\/swift-keyboard-visibility\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Swift) Keyboard Visibility - S(ei)mith.io\" \/>\n<meta property=\"og:description\" content=\"This post is about doing things when your keyboard is visible or not. You might want to have some animation when your keyboard comes into view, or you might want an alert to pop up. Whatever it may be, here\u2019s a snippet for that. In the example below, I just have a&nbsp;UITextView&nbsp;and a&nbsp;UIButton&nbsp;set up so [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/seimith.io\/pt\/2017\/01\/07\/swift-keyboard-visibility\/\" \/>\n<meta property=\"og:site_name\" content=\"S(ei)mith.io\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-07T21:23:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-20T00:38:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"700\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Smith\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smith\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/\"},\"author\":{\"name\":\"Smith\",\"@id\":\"https:\\\/\\\/seimith.io\\\/#\\\/schema\\\/person\\\/315065130ef0017986daf9a1127ce80a\"},\"headline\":\"(Swift) Keyboard Visibility\",\"datePublished\":\"2017-01-07T21:23:00+00:00\",\"dateModified\":\"2024-07-20T00:38:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/\"},\"wordCount\":170,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"keywords\":[\"Code\"],\"articleSection\":[\"Post\"],\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/\",\"url\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/\",\"name\":\"(Swift) Keyboard Visibility - S(ei)mith.io\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"datePublished\":\"2017-01-07T21:23:00+00:00\",\"dateModified\":\"2024-07-20T00:38:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/#\\\/schema\\\/person\\\/315065130ef0017986daf9a1127ce80a\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#primaryimage\",\"url\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"contentUrl\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"width\":700,\"height\":700},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2017\\\/01\\\/07\\\/swift-keyboard-visibility\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/seimith.io\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Swift) Keyboard Visibility\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/seimith.io\\\/#website\",\"url\":\"https:\\\/\\\/seimith.io\\\/\",\"name\":\"s(ei)mith.io\",\"description\":\"Ramblings written by\u00a0Smith Suth\u00a0who works at the intersection of Engineering, Product, and Design.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/seimith.io\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/seimith.io\\\/#\\\/schema\\\/person\\\/315065130ef0017986daf9a1127ce80a\",\"name\":\"Smith\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g\",\"caption\":\"Smith\"},\"sameAs\":[\"https:\\\/\\\/seimith.io\"],\"url\":\"https:\\\/\\\/seimith.io\\\/pt\\\/author\\\/seimithsuth\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Swift) Keyboard Visibility - S(ei)mith.io","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/seimith.io\/pt\/2017\/01\/07\/swift-keyboard-visibility\/","og_locale":"pt_BR","og_type":"article","og_title":"(Swift) Keyboard Visibility - S(ei)mith.io","og_description":"This post is about doing things when your keyboard is visible or not. You might want to have some animation when your keyboard comes into view, or you might want an alert to pop up. Whatever it may be, here\u2019s a snippet for that. In the example below, I just have a&nbsp;UITextView&nbsp;and a&nbsp;UIButton&nbsp;set up so [&hellip;]","og_url":"https:\/\/seimith.io\/pt\/2017\/01\/07\/swift-keyboard-visibility\/","og_site_name":"S(ei)mith.io","article_published_time":"2017-01-07T21:23:00+00:00","article_modified_time":"2024-07-20T00:38:47+00:00","og_image":[{"width":700,"height":700,"url":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","type":"image\/webp"}],"author":"Smith","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smith","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#article","isPartOf":{"@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/"},"author":{"name":"Smith","@id":"https:\/\/seimith.io\/#\/schema\/person\/315065130ef0017986daf9a1127ce80a"},"headline":"(Swift) Keyboard Visibility","datePublished":"2017-01-07T21:23:00+00:00","dateModified":"2024-07-20T00:38:47+00:00","mainEntityOfPage":{"@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/"},"wordCount":170,"commentCount":0,"image":{"@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#primaryimage"},"thumbnailUrl":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","keywords":["Code"],"articleSection":["Post"],"inLanguage":"pt-BR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/","url":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/","name":"(Swift) Keyboard Visibility - S(ei)mith.io","isPartOf":{"@id":"https:\/\/seimith.io\/#website"},"primaryImageOfPage":{"@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#primaryimage"},"image":{"@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#primaryimage"},"thumbnailUrl":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","datePublished":"2017-01-07T21:23:00+00:00","dateModified":"2024-07-20T00:38:47+00:00","author":{"@id":"https:\/\/seimith.io\/#\/schema\/person\/315065130ef0017986daf9a1127ce80a"},"breadcrumb":{"@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#primaryimage","url":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","contentUrl":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","width":700,"height":700},{"@type":"BreadcrumbList","@id":"https:\/\/seimith.io\/2017\/01\/07\/swift-keyboard-visibility\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/seimith.io\/"},{"@type":"ListItem","position":2,"name":"(Swift) Keyboard Visibility"}]},{"@type":"WebSite","@id":"https:\/\/seimith.io\/#website","url":"https:\/\/seimith.io\/","name":"s(ei)mith.io","description":"Ramblings written by\u00a0Smith Suth\u00a0who works at the intersection of Engineering, Product, and Design.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/seimith.io\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Person","@id":"https:\/\/seimith.io\/#\/schema\/person\/315065130ef0017986daf9a1127ce80a","name":"Smith","image":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/secure.gravatar.com\/avatar\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g","caption":"Smith"},"sameAs":["https:\/\/seimith.io"],"url":"https:\/\/seimith.io\/pt\/author\/seimithsuth\/"}]}},"_links":{"self":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":1,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts\/76\/revisions\/77"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/media\/288"}],"wp:attachment":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}