Thursday, July 30, 2015

UIActivityIndicatorView - Show an Activity Indicator Programmatically

Update: July 8, 2017 (Swift 3.1 + Xcode 8.3.3)
Original Post: July 30, 2015 (Swift 1 + Xcode 6.4)

Complete the code as below:

Update: July 8, 2017 (Swift 3.1 + Xcode 8.3.3)

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
        myActivityIndicator.center = view.center
        myActivityIndicator.startAnimating()
        view.addSubview(myActivityIndicator)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Original Post: July 30, 2015 (Swift 1 + Xcode 6.4)

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        var myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        myActivityIndicator.center = view.center
        myActivityIndicator.startAnimating()
        view.addSubview(myActivityIndicator)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

The result:

Saturday, July 4, 2015

Create an app with three tabs using the storyboard

This tutorial shows how to create tab bar items with custom icons using the storyboard.

1. Create a new project and select iOS/Application/Tabbed Application template:



2. Create a new file named ThirdViewController.swift and change the content as:

import UIKit

class ThirdViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

}

3. Drag a ViewController to the storyboard. Select the new ViewController and make it as a ThirdViewController class in the Identity Inspector.


4. Drag a label to ThirdViewController in the storyboard. Drag it to the center. Select 'Align' at the bottom right of the interface builder, click for both horizontal and vertical center in container, and select Add 2 Constraints.


5. Control-drag from the Tab Bar Controller to the Third View Controller. Select Relationship Segue/view controllers.


6. Now the Third View Controller is renamed as Item. Select it. Change  Attributes Inspector/View Controller/Title as Third.


7. Select Item under Third/View and change Attributes Inspector/Bar Item/Title as Third.


8. Select 'Preview' as a tool to make three icons with transparent background. Make the size of the icons as 25x25 with BatchImageResizerLite. Drag the icons to the project:


9. Select Tab Bar Item under  First View Controller Scene/First View Controller/View and select an image under Attributes Inspector/Bar Item/Image.
Repeat this process for the Tab Bar Items inside the Second and Third Scenes.


10. Run the app. Try tap on 1, 2, and 3.



Wednesday, July 1, 2015

Display a web page with UIWebView and insert a JavaScript file using stringByEvaluatingJavaScriptFromString

This is a tutorial about inserting JavaScript code into the web page being read.

1. Select the storyboard and disable the Use Auto Layout and Use Size Classes option.


2. Drag a UIWebView to the storyboard.


3. Control-drag the UIWebView to ViewControl.swift

    @IBOutlet weak var myWebView: UIWebView!

4. Create an index.html file in the project:

<HTML>
    <HEAD>
        <TITLE>My Title</TITLE>
    </HEAD>
    <BODY>
        <H2><CENTER>Hello World</CENTER></H2>
    </BODY>
</HTML>

5. Create a JavaScript.js file in the project:


alert('JavaScript!');

6. Complete the ViewController.swift file as below:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var myWebView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var myPath = NSBundle.mainBundle().pathForResource("index", ofType: "html")
        myWebView.delegate = self //For webViewDidFinishLoad to be called
        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: myPath!)!))
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        let jsPath = NSBundle.mainBundle().pathForResource("JavaScript", ofType: "js")
        let jsContent = NSString(contentsOfFile: jsPath!, encoding: NSUTF8StringEncoding, error: nil) as! String
        
        myWebView.stringByEvaluatingJavaScriptFromString(jsContent)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

7. The alert window:

The HTML file is displayed in UIWebView: