UIKit is “the cornerstone of creating user interfaces in all iOS apps”, according to the Apple documentation. Here are some notes on some elements of UIKit…
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, and if the title of the button is a ‘+’, sets the title to “0”:
for view in self.view.subviews as! [UIView] { // 1 if let btn = view as? UIButton { // 2 if btn.currentTitle == "+" { // 3 btn.setTitle("O", forState: .Normal) // 4 } } }
Peculiar to Swift/IOS are ‘as!‘, ‘as?‘, UIView, UIButton and subviews.
- Apple has documented the UIView class and Windows and Views.
- For an explanation of why ‘as!‘ is used, helpful is Apple’s documentation on Type Casting, and a page of their blog.
- Optionals is explained at Apple, and by Andrew Wagner.
Referring to the code above:
- The currentTitle property returns the title that is displayed on the UIButton. See the Apple documentation for other title properties.
- The setTitle method takes the following forState parameters: .Normal, .Highlighted, .Disabled, .Selected, .Application, and .Reserved.
Creating an Alert Message
The order of events is:
- Create and define a UIAlertController
- Create and define a UIAlertAction
- Pass the action to the UIAlertController using it’s addAction method
- Show the Alert Message using presentViewController.
- And to close the Alert Message (with animation, use true below), call
dismissViewControllerAnimated(true, completion: nil)
Note that presentViewController returns immediately, and programme execution continues with the statement following presentViewController. That is, Alert Messages are shown asynchronously.
- UIAlertController, 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 style. Possible styles are .Alert, and .ActionSheet. For example:
let alert = UIAlertController(title: "Hello, World", message: "This is my first app!", preferredStyle: .Alert)
- UIAlertAction, is the action assigned to a button in the Alert Message. The parameters passed to the button are the title of the button, the style of the button, and a function to perform an action when the user touches the button. Possible styles are .Default, .Cancel, and .Destructive. .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:
func someHandler(alert: UIAlertAction!) {
// Do something...
}
let action = UIAlertAction(title: "Awesome!", style: .Default, handler: someHandler)
You tell the Alert Controller about the Alert Action by passing the action using the addAction method. For example, using the above two examples:
alert.addAction(action)
- presentViewController commands IOS to display the Alert Message and the Alert Action(s) defined above. The parameters to pass to presentViewController 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:
presentViewController(alert, animated: true, completion: nil)
Dimensions of IOS devices
- iPhone 3GS and older (3.5″) – 320×480 points – 1 pixel / point
- iPhone 4 and 4S (3.5″) – 320×480 points – 2 pixels / point
- iPhone 5, 5c and 5s (4″) – 320×568 points – 2 pixels / point
- iPhone 6 (4.7″) – 375×667 points – 2 pixels / point
- iPhone 6 Plus (5.5″) – 414×736 points – 3 pixels / point
- iPad (all models) (7.9″ and 9.7″) – 768×1024 points – 2 pixels / point (although iPad 2 and mini only have 1 pixel / point)
Note, when creating images, there are 2 pixels per point. For example, a background image for the iPhone 4S will be 640×960 pixels.
For app icon sizes (and others) see IOS Human Interface Guidelines.
Adding images to Sliders
For example:
let thumbImageNormal = UIImage(named: "SliderThumb-Normal") slider.setThumbImage(thumbImageNormal, forState: .Normal) let thumbImageHighlighted = UIImage(named: "SliderThumb-Highlighted") slider.setThumbImage(thumbImageHighlighted, forState: .Highlighted) let insets = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14) if let trackLeftImage = UIImage(named: "SliderTrackLeft") { let trackLeftResizable = trackLeftImage.resizableImageWithCapInsets(insets) slider.setMinimumTrackImage(trackLeftResizable, forState: .Normal) } if let trackRightImage = UIImage(named: "SliderTrackRight") { let trackRightResizable = trackRightImage.resizableImageWithCapInsets(insets) slider.setMaximumTrackImage(trackRightResizable, forState: .Normal) }
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 Natasha The Robot for more information.