Created
May 13, 2015 00:27
-
-
Save codebeaulieu/a9972f95bbaf08e0bbfb to your computer and use it in GitHub Desktop.
Stack Overflow Question: Is it possible to conditionally specify portrait or landscape mode?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Browser | |
// | |
// Created by Dan Beaulieu on 5/12/15. | |
// Copyright (c) 2015 Dan Beaulieu. All rights reserved. | |
// | |
import UIKit | |
import WebKit | |
class ViewController: UIViewController, UITextFieldDelegate, WKNavigationDelegate { | |
var webView: WKWebView | |
var bounce : Bool = AppDelegate().userDefaults.valueForKey("Disable Bounce") as! Bool | |
var scroll : Bool = AppDelegate().userDefaults.valueForKey("Disable Scroll") as! Bool | |
var hideNavBar : Bool = AppDelegate().userDefaults.valueForKey("Hide Navigation Bar") as! Bool | |
var storedUrl : String = AppDelegate().userDefaults.valueForKey("Web Address") as! String | |
var statusBar : Bool = AppDelegate().userDefaults.valueForKey("Hide Status Bar") as! Bool | |
var statusDefault : Bool = AppDelegate().userDefaults.valueForKey("Light Status Bar") as! Bool | |
var portraitMode : Bool = AppDelegate().userDefaults.valueForKey("Portrait Mode") as! Bool | |
var landscapeMode : Bool = AppDelegate().userDefaults.valueForKey("Landscape Mode") as! Bool | |
@IBOutlet weak var barView: UIView! | |
@IBOutlet weak var urlField: UITextField! | |
@IBOutlet weak var backButton: UIBarButtonItem! | |
@IBOutlet weak var progressView: UIProgressView! | |
required init(coder aDecoder: NSCoder) { | |
self.webView = WKWebView(frame: CGRectZero) | |
super.init(coder: aDecoder) | |
self.webView.navigationDelegate = self | |
} | |
override func viewWillAppear(animated: Bool) { | |
if (hideNavBar == true) { | |
barView.hidden = true | |
urlField.hidden = true | |
self.navigationController?.setNavigationBarHidden(true, animated: true) | |
} else { | |
barView.hidden = false | |
urlField.hidden = false | |
self.navigationController?.setNavigationBarHidden(false, animated: true) | |
} | |
self.navigationController?.setToolbarHidden(true, animated: true) | |
// status bar settings | |
if statusDefault == true { | |
AppDelegate().setStatusBarStyleToLight() | |
} else { | |
AppDelegate().setStatusBarStyleToDefault() | |
} | |
if statusBar == true { | |
AppDelegate().hideStatusBar() | |
} else { | |
AppDelegate().showStatusBar() | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
barView.frame = CGRect(x:0, y: 0, width: view.frame.width, height: 30) | |
view.insertSubview(webView, belowSubview: progressView) | |
webView.setTranslatesAutoresizingMaskIntoConstraints(false) | |
let height = NSLayoutConstraint(item: webView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: 0) | |
let width = NSLayoutConstraint(item: webView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0) | |
view.addConstraints([height, width]) | |
webView.addObserver(self, forKeyPath: "loading", options: .New, context: nil) | |
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil) | |
let url = NSURL(string:storedUrl) | |
let request = NSURLRequest(URL:url!) | |
webView.loadRequest(request) | |
urlField.text = storedUrl | |
evaluateScrollSettings() | |
backButton.enabled = false | |
} | |
override func shouldAutorotate() -> Bool { | |
if portraitMode == true || landscapeMode == true { | |
return false | |
} else { | |
return true | |
} | |
} | |
override func supportedInterfaceOrientations() -> Int { | |
if portraitMode == true { | |
return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) | |
} else { | |
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) | Int(UIInterfaceOrientationMask.LandscapeRight.rawValue) | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { | |
barView.frame = CGRect(x:0, y: 0, width: size.width, height: 30) | |
} | |
func textFieldShouldReturn(textField: UITextField) -> Bool { | |
urlField.resignFirstResponder() | |
webView.loadRequest(NSURLRequest(URL:NSURL(string: urlField.text)!)) | |
return false | |
} | |
@IBAction func back(sender: UIBarButtonItem) { | |
webView.goBack() | |
} | |
func evaluateScrollSettings() { | |
if scroll == false { | |
webView.scrollView.scrollEnabled = true | |
} else { | |
webView.scrollView.scrollEnabled = false | |
} | |
if bounce == false { | |
webView.scrollView.bounces = true | |
} else { | |
webView.scrollView.bounces = false | |
} | |
} | |
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<()>) { | |
if (keyPath == "loading") { | |
backButton.enabled = webView.canGoBack | |
//forwardButton.enabled = webView.canGoForward | |
} | |
if (keyPath == "estimatedProgress") { | |
progressView.hidden = webView.estimatedProgress == 1 | |
progressView.setProgress(Float(webView.estimatedProgress), animated: true) | |
} | |
} | |
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) { | |
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .Alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil)) | |
presentViewController(alert, animated: true, completion: nil) | |
} | |
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { | |
progressView.setProgress(0.0, animated: false) | |
} | |
override func prefersStatusBarHidden() -> Bool { | |
return statusBar | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment