{"id":487,"date":"2015-06-18T17:51:12","date_gmt":"2015-06-18T06:51:12","guid":{"rendered":"http:\/\/gregvinall.com\/wp\/?page_id=487"},"modified":"2015-06-18T22:30:37","modified_gmt":"2015-06-18T11:30:37","slug":"uikit","status":"publish","type":"page","link":"https:\/\/gregvinall.com\/wp\/swift\/uikit\/","title":{"rendered":"UIKit"},"content":{"rendered":"<p>UIKit is\u00a0&#8220;the cornerstone of creating user interfaces in all iOS apps&#8221;, according to the Apple documentation. Here are some notes on some elements of UIKit&#8230;<\/p>\n<h3>Iterating through the views<\/h3>\n<p>To iterate through all the views\/controls in a ViewController, use a loop, through the subviews. The following code searches for all the buttons in the ViewController, and if the title of the button is a &#8216;+&#8217;, sets the title to &#8220;0&#8221;:<\/p>\n<pre>for view in self.view.subviews as! [UIView] {     \/\/ 1\r\n    if let btn = view as? UIButton {              \/\/ 2\r\n        if btn.currentTitle == \"+\" {              \/\/ 3\r\n            btn.setTitle(\"O\", forState: .Normal)  \/\/ 4\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Peculiar to Swift\/IOS are &#8216;<strong>as!<\/strong>&#8216;, &#8216;<strong>as?<\/strong>&#8216;,\u00a0<strong>UIView<\/strong>,\u00a0<strong>UIButton<\/strong>\u00a0and\u00a0<strong>subviews<\/strong>.<\/p>\n<ul>\n<li>Apple has documented the\u00a0<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/UIKit\/Reference\/UIView_Class\/\" target=\"_blank\">UIView class<\/a>\u00a0and\u00a0<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/WindowsViews\/Conceptual\/ViewPG_iPhoneOS\/Introduction\/Introduction.html#\/\/apple_ref\/doc\/uid\/TP40009503\" target=\"_blank\">Windows and Views<\/a>.<\/li>\n<li>For an explanation of why &#8216;<strong>as!<\/strong>&#8216; is used, helpful is\u00a0Apple&#8217;s documentation on\u00a0<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/TypeCasting.html\" target=\"_blank\">Type Casting<\/a>, and a\u00a0<a href=\"https:\/\/developer.apple.com\/swift\/blog\/?id=23\" target=\"_blank\">page of their blog<\/a>.<\/li>\n<li>\u00a0Optionals is explained at\u00a0<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/TheBasics.html\" target=\"_blank\">Apple<\/a>, and by\u00a0<a href=\"http:\/\/www.drewag.me\/posts\/what-is-an-optional-in-swift\" target=\"_blank\">Andrew Wagner<\/a>.<\/li>\n<\/ul>\n<p>Referring to the code above:<\/p>\n<ol>\n<li>The\u00a0<strong>currentTitle<\/strong>\u00a0property returns the title that is displayed on the UIButton. See the\u00a0<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/UIKit\/Reference\/UIButton_Class\/#\/\/apple_ref\/occ\/instp\/UIButton\/currentTitle\" target=\"_blank\">Apple documentation<\/a>\u00a0for other title properties.<\/li>\n<li>The\u00a0<strong>setTitle<\/strong>\u00a0method takes the following forState parameters: .Normal, .Highlighted, .Disabled, .Selected, .Application, and .Reserved.<\/li>\n<\/ol>\n<h3>Creating an Alert Message<\/h3>\n<p>The order of events is:<\/p>\n<ol>\n<li>Create and define a\u00a0<strong>UIAlertController<\/strong><\/li>\n<li>Create and define a\u00a0<strong>UIAlertAction<\/strong><\/li>\n<li>Pass the action to the\u00a0<strong>UIAlertController<\/strong>\u00a0using it&#8217;s\u00a0<strong>addAction<\/strong>\u00a0method<\/li>\n<li>Show the Alert Message using\u00a0<strong>presentViewController<\/strong>.<\/li>\n<li>And to close the Alert Message (with animation, use true below), call<\/li>\n<\/ol>\n<pre>dismissViewControllerAnimated(true, completion: nil)<\/pre>\n<p>Note that\u00a0<strong>presentViewController<\/strong>\u00a0returns immediately, and programme execution continues with the statement following\u00a0<strong>presentViewController<\/strong>. That is, Alert Messages are shown asynchronously.<\/p>\n<ul>\n<li><strong>UIAlertController<\/strong>, which is available in IOS 8.0 and later, is the Alert Message Controller, and displays an alert message to the user, with the provided title, message, and\u00a0style. Possible styles are\u00a0<strong>.Alert<\/strong>, and\u00a0<strong>.ActionSheet<\/strong>. For example:<\/li>\n<\/ul>\n<pre>let alert = UIAlertController(title: \"Hello, World\", message: \"This is my first app!\", preferredStyle: .Alert)<\/pre>\n<ul>\n<li><strong>UIAlertAction<\/strong>, is the action assigned to a button in the Alert Message. The parameters passed to the button are the title of the button, \u00a0the style of the button, and a function to perform an action when the user touches the button. Possible styles are\u00a0<strong>.Default<\/strong>,\u00a0<strong>.Cancel<\/strong>, and\u00a0<strong>.Destructive<\/strong>. .Cancel will display a style which indicates to the user that the operation will be cancelled and things will remain unchanged, while .Destructive will display a style which indicates to the user that data will be changed or deleted. For example:<\/li>\n<\/ul>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"pln\">func someHandler<\/span><span class=\"pun\">(<\/span><span class=\"pln\">alert<\/span><span class=\"pun\">:<\/span> <span class=\"typ\">UIAlertAction<\/span><span class=\"pun\">!)<\/span> <span class=\"pun\">{<\/span>\r\n    <span class=\"com\">\/\/ Do something...<\/span>\r\n<span class=\"pun\">}\r\n\r\n<\/span><\/code>let action = UIAlertAction(title: \"Awesome!\", style: .Default, handler: someHandler)<\/pre>\n<p>You tell the Alert Controller about the Alert Action by passing the action using the addAction method. For example, using the above two examples:<\/p>\n<pre>alert.addAction(action)<\/pre>\n<ul>\n<li><strong>presentViewController<\/strong>\u00a0commands IOS to display the Alert Message and the Alert Action(s) defined above. The parameters to pass to\u00a0presentViewController are the configured UIAlertController, whether to animate the presentation (true or false), and a function (with no parameters and does not return a value) to execute upon completion of the presentation. For example, using the examples above:<\/li>\n<\/ul>\n<pre>presentViewController(alert, animated: true, completion: nil)<\/pre>\n<h3>Dimensions of IOS devices<\/h3>\n<ul>\n<li>iPhone 3GS and older (3.5&#8243;) &#8211; 320&#215;480 points &#8211; 1 pixel \/ point<\/li>\n<li>iPhone 4 and 4S (3.5&#8243;) &#8211; 320&#215;480\u00a0points &#8211; 2 pixels \/ point<\/li>\n<li>iPhone 5, 5c and 5s (4&#8243;) &#8211; 320&#215;568\u00a0points\u00a0&#8211; 2 pixels \/ point<\/li>\n<li>iPhone 6 (4.7&#8243;) &#8211; 375&#215;667\u00a0points\u00a0&#8211; 2 pixels \/ point<\/li>\n<li>iPhone 6 Plus (5.5&#8243;) &#8211; 414&#215;736\u00a0points\u00a0&#8211; 3 pixels \/ point<\/li>\n<li>iPad (all models) (7.9&#8243; and 9.7&#8243;) &#8211; 768&#215;1024\u00a0points\u00a0&#8211; 2 pixels \/ point (although iPad 2 and mini only have 1 pixel \/ point)<\/li>\n<\/ul>\n<p>Note, when creating images, there are 2 pixels per point. For example, a background image for the iPhone 4S will be 640&#215;960 pixels.<\/p>\n<p>For app icon sizes (and others) see <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/UserExperience\/Conceptual\/MobileHIG\/IconMatrix.html\" target=\"_blank\">IOS Human Interface Guidelines<\/a>.<\/p>\n<h3 class=\"column\">Adding images to Sliders<\/h3>\n<p>For example:<\/p>\n<pre>let thumbImageNormal = UIImage(named: \"SliderThumb-Normal\") slider.setThumbImage(thumbImageNormal, forState: .Normal)\r\n\r\nlet thumbImageHighlighted = UIImage(named: \"SliderThumb-Highlighted\")\r\n\r\nslider.setThumbImage(thumbImageHighlighted, forState: .Highlighted)\r\n\r\nlet insets = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)\r\n\r\nif let trackLeftImage = UIImage(named: \"SliderTrackLeft\") {\r\n    let trackLeftResizable =\r\n        trackLeftImage.resizableImageWithCapInsets(insets)\r\n    slider.setMinimumTrackImage(trackLeftResizable, forState: .Normal)\r\n}\r\n\r\nif let trackRightImage = UIImage(named: \"SliderTrackRight\") {\r\n    let trackRightResizable =\r\n        trackRightImage.resizableImageWithCapInsets(insets)\r\n    slider.setMaximumTrackImage(trackRightResizable, forState: .Normal)\r\n}<\/pre>\n<p>The above code sets normal and highlighted images for the thumb, and sets different images for the left and right side of the track. Insets are the size of the edges not to be altered when stretching the image to fit the track. See <a href=\"http:\/\/natashatherobot.com\/ios-stretchable-button-uiedgeinsetsmake\/\" target=\"_blank\">Natasha The Robot<\/a> for more information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UIKit is\u00a0&#8220;the cornerstone of creating user interfaces in all iOS apps&#8221;, according to the Apple documentation. Here are some notes on some elements of UIKit&#8230; Iterating through the views To iterate through all the views\/controls in a ViewController, use a loop, through the subviews. The following code searches for all the buttons in the ViewController, &hellip; <a href=\"https:\/\/gregvinall.com\/wp\/swift\/uikit\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;UIKit&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":341,"parent":336,"menu_order":5,"comment_status":"open","ping_status":"open","template":"","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-487","page","type-page","status-publish","has-post-thumbnail","hentry"],"featured_image_src":"https:\/\/i0.wp.com\/gregvinall.com\/wp\/wp-content\/uploads\/2015\/06\/Swift-Cover-Photo1.jpg?resize=600%2C153&ssl=1","featured_image_src_square":"https:\/\/i0.wp.com\/gregvinall.com\/wp\/wp-content\/uploads\/2015\/06\/Swift-Cover-Photo1.jpg?resize=600%2C153&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P7Q68N-7R","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/pages\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":14,"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/pages\/487\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/pages\/487\/revisions\/522"}],"up":[{"embeddable":true,"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/pages\/336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/media\/341"}],"wp:attachment":[{"href":"https:\/\/gregvinall.com\/wp\/wp-json\/wp\/v2\/media?parent=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}