Update: July 8, 2017 (Swift 3.1 + Xcode 8.3.3)
Original Post: September 15, 2015
This post shows how to embed the view controller in a navigation controller.
Original Post: September 15, 2015
This post shows how to embed the view controller in a navigation controller.
Update: July 8, 2017 (Swift 3.1 + Xcode 8.3.3)
1. Modify func application(_ application: UIApplication, didFinishLaunchingWithOptions...) in AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let nav = UINavigationController()
let mainWiew = ViewController(nibName: nil, bundle: nil)
nav.viewControllers = [mainWiew]
nav.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().barTintColor = UIColor.blue
UINavigationBar.appearance().tintColor = UIColor.white
//White status font
UINavigationBar.appearance().barStyle = UIBarStyle.blackTranslucent
self.window!.rootViewController = nav
self.window?.makeKeyAndVisible()
//Black status background
let statusBar = UIView()
statusBar.frame = CGRect(x: 0, y: 0, width: 320, height: 20)
statusBar.backgroundColor = UIColor.black
self.window?.rootViewController?.view.addSubview(statusBar)
return true
}
2. Modify ViewController.swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Title"
let statusView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 20))
statusView.backgroundColor = UIColor.black
view.addSubview(statusView)
let myView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
myView.backgroundColor = UIColor.white
view.addSubview(myView)
let barBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.compose, target: self, action: #selector(pressed))
self.navigationItem.setRightBarButton(barBtn, animated: true)
}
func pressed() {
print("Pressed")
let newVC = SecondViewController()
self.navigationController?.pushViewController(newVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
3. Create a new file called SecondViewController.swift by right-clicking and choosing New File -> iOS -> Cocoa Touch.
Choose the new class as a subclass of UIViewController and name it as .
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
myView.backgroundColor = UIColor.white
view.addSubview(myView)
let myLabel = UILabel(frame: CGRect(x: 50, y: 100, width: 150, height: 20))
myLabel.text = "Hello"
view.addSubview(myLabel)
navigationItem.title = "123"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Original Post: September 15, 2015
1. Modify AppDelegate.swift:
1. Modify AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var nav = UINavigationController()
var mainWiew = ViewController(nibName: nil, bundle: nil)
nav.viewControllers = [mainWiew]
nav.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().barTintColor = UIColor.blueColor()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
//White status font
UINavigationBar.appearance().barStyle = UIBarStyle.BlackTranslucent
self.window!.rootViewController = nav
self.window?.makeKeyAndVisible()
//Black status background
var statusBar = UIView()
statusBar.frame = CGRectMake(0, 0, 320, 20)
statusBar.backgroundColor = UIColor.blackColor()
self.window?.rootViewController?.view.addSubview(statusBar)
return true
}
2. Modify ViewController.swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Title"
let statusView = UIView(frame: CGRectMake(0, 0, view.bounds.width, 20))
statusView.backgroundColor = UIColor.blackColor()
view.addSubview(statusView)
var myView = UIView()
myView.backgroundColor = UIColor.whiteColor()
myView.frame = CGRectMake(0, 0, view.bounds.width, view.bounds.height)
view.addSubview(myView)
var barBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Compose, target: self, action: "pressed:")
self.navigationItem.setRightBarButtonItem(barBtn, animated: true)
}
func pressed(sender: UIButton) {
println("Pressed")
let newVC = SecondViewController()
self.navigationController?.pushViewController(newVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
3. Create a new file called SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var myView = UIView()
myView.backgroundColor = UIColor.whiteColor()
myView.frame = CGRectMake(0, 0, view.bounds.width, view.bounds.height)
view.addSubview(myView)
var myLabel = UILabel()
myLabel.text = "Hello"
myLabel.frame = CGRectMake(50, 100, 150, 20)
view.addSubview(myLabel)
navigationItem.title = "123"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The result:
After clicking the 'Compose' button:
No comments:
Post a Comment