How to Programmatically Scroll iOS WKWebView, swift 4 - swift

So my question is: After a website(any websites on the www) has been loaded up in my iOS webview app how to make the app Programmatically Scroll vertically to any of the contents thats out of view?
// ViewController.swift
// myWebView
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var myWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
myWebView.load(urlRequest)
}
func webViewDidFinishLoad(_ webView: WKWebView) {
let scrollPoint = CGPoint(x: 0, y: webView.scrollView.contentSize.height - webView.frame.size.height)
webView.scrollView.setContentOffset(scrollPoint, animated: true )
}
}

This will also account for the possible horizontal offset of the origin of the scrollView:
var scrollPoint = self.view.convert(CGPoint(x: 0, y: 0), to: webView.scrollView)
scrollPoint = CGPoint(x: scrollPoint.x, y: webView.scrollView.contentSize.height - webView.frame.size.height)
webView.scrollView.setContentOffset(scrollPoint, animated: true)

You can manipulate the Scroll View of the webview. Below there's an example to scroll to the bottom of the view:
let scrollPoint = CGPoint(x: 0, y: yourWebView.scrollView.contentSize.height - webView.frame.size.height)
webView.scrollView.setContentOffset(scrollPoint, animated: true)

If you want to get notify your wbeview reach to top or bottom use this extension, I made it long time ago for wkwebview. webview reach top or bottom

Related

In WKWebView how do I make it so it fits the margins set within storyboard?

For the webkit I'm having problems setting the size of it. I'm using Swift with Storyboard and I've tried some other solutions but none have worked here`class ViewController: UIViewController, UIGestureRecognizerDelegate , UIWebViewDelegate, WKNavigationDelegate {
let url = URL(string: "google.com")!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRect( x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height - 20 ), configuration: WKWebViewConfiguration() )
self.view.addSubview(webView)
webView.load(URLRequest(url: url))
self.webView.allowsBackForwardNavigationGestures = true
}`
PS. Sorry for the notation, I had tried to fixed it but Stack is being weird

UIScrollView not zooming: What am I missing?

I can scroll, but not zoom. I've seen a lot of identical looking code while searching, but still there's no zoom.
The pinchGestureRecognizer is not nil.
viewForZoomingInScrollView(scrollView: UIScrollView) is not being called.
scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) reports scale = 1.0.
What am I missing here?
(We need to do this programmatically rather than in IB).
import UIKit
class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = imageView.bounds.size
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
scrollView.minimumZoomScale = 0.1
scrollView.maximumZoomScale = 4.0
scrollView.zoomScale = 1.0
}
//delegate
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
}
Did you convert your codes from an older version of Swift? Because the delegate function for returning the view for zooming is now
func viewForZooming(in scrollView: UIScrollView) -> UIView?

NSButton action not working from custom NSView

I have my Push button in subview called AddContactViewController
And I'm showing it in ScrollView in another ViewController called AddContactScrollViewController.
I'm setting action for my button in AddContactViewController
But, when I click my button nothing happens.
Here are the screenshots my IB. And below is the code.
Main view controller
SubView
import Cocoa
class AddContactScrollViewController: NSViewController {
#IBOutlet weak var scrollView: NSScrollView!
var contentView: NSView?
override func viewDidLoad() {
super.viewDidLoad()
contentView = NSView(frame: NSRect(x: 0, y: 0, width: self.view.frame.width, height: 1123))
contentView!.wantsLayer = true
contentView!.layer?.backgroundColor = NSColor.clear.cgColor
let tempVC = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "addAddress")) as! AddAddressViewController
vc.view.setFrameOrigin(NSPoint(x: 0, y: 0))
vc.view.setFrameSize(NSSize(width: 1200, height: 1123))
vc.view.wantsLayer = true
contentView!.addSubview(vc.view)
scrollView.documentView = contentView
// scroll to the top
if let documentView = scrollView.documentView {
documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height))
}
}
override func viewDidAppear() {
}
}
import Cocoa
class AddContactVeiwController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func addPhoneButtonClicked(_ sender: Any) {
print("phone button clicked")
}

Set initial ScrollView

I want to set "ViewController2" as initial page in the ScrollView but changing setContentOffset(CGPointMake(0,0), animated: false) sets the first page as initial, i tried modified it with different numbers but it just messed up the pages, any tips?
import UIKit
class ViewController: UIViewController {
#IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.bounces = false
scrollView.pagingEnabled = true
scrollView.setContentOffset(CGPointMake(0,0), animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLayoutSubviews() {
initScrollView()
}
func initScrollView(){
let viewController1 = storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1
viewController1.willMoveToParentViewController(self)
viewController1.view.frame = scrollView.bounds
let viewController2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
viewController2.willMoveToParentViewController(self)
viewController2.view.frame.size = scrollView.frame.size
viewController2.view.frame.origin = CGPoint(x: view.frame.width, y: 0)
let viewController3 = storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3
viewController3.willMoveToParentViewController(self)
viewController3.view.frame.size = scrollView.frame.size
viewController3.view.frame.origin = CGPoint(x: view.frame.width * 2, y: 0)
scrollView.contentSize = CGSize(width: 3 * scrollView.frame.width, height: scrollView.frame.height)
scrollView.addSubview(viewController3.view)
self.addChildViewController(viewController3)
viewController3.didMoveToParentViewController(self)
scrollView.addSubview(viewController2.view)
self.addChildViewController(viewController2)
viewController2.didMoveToParentViewController(self)
scrollView.addSubview(viewController1.view)
self.addChildViewController(viewController1)
viewController1.didMoveToParentViewController(self)
}}
Try to de-select the scroll view inset adjustment... in vc2:
By putting scrollView.setContentOffset(CGPointMake(375,0), animated: false) in viewDidlayoutSubViews it loads the next view, note that i am using iphone 4,7 inch screen so the width of a viewController is 375. change 375 to the width of your viewController, for example if you wanna load the last page first you do add 375 to 375 and so on..
override func viewDidLayoutSubviews() {
initScrollView()
scrollView.setContentOffset(CGPointMake(375,0), animated: false)
}

How to hide keyboard with programmatically created uitextfield

I've programmatically created a uitextfield called "myTextField" in "viewDidLoad".
When I create a function to hide the keyboard when the user taps anywhere on the screen, "myTextField" isn't recognized by xcode. Is it because the textfield is created inside "viewDidLoad"?
Is there any way to access and use "myTextField" outside "viewDidLoad"?
Update: I finally had a chance to go home and copy my code to show what I mean. Here is my code:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Prepare keyboard notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardDidHideNotification, object: nil);
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width;
let screenHeight = screenSize.height;
let myTextView : UITextField = UITextField(
frame : CGRect(x:10, y:(screenHeight/2), width:(screenWidth-20), height: (screenHeight/3) ) )
myTextView.backgroundColor = UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )
self.scrollView.addSubview( myTextView )
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Call this method somewhere in your view controller setup code.
func keyboardWillShow(sender: NSNotification) {
let dict:NSDictionary = sender.userInfo as NSDictionary
let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
self.scrollView.contentOffset = CGPoint(x: 0, y: rect.height)
}
func keyboardWillHide(sender: NSNotification) {
let dict:NSDictionary = sender.userInfo as NSDictionary
let s:NSValue = dict.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
self.scrollView.contentOffset = CGPoint(x: 0, y: 0)
}
I've created a UITextView programmatically because I'm struggling with auto layout in Xcode 6. In the meantime I've updated to Xcode beta 5 (from 4) and now even my scrollview is acting weird. So maybe I have to create it with code as well.
How can I access my textview outside of function viewDidLoad?
Declare it as a property of that class, then you can access it from anywhere. Otherwise its scope is limited to the viewDidLoad function and you can't access it from outside.
class YourClass {
var myTextField:UITextField
override func viewDidLoad() {
//init myTextField
}
func test() {
//do something with myTextField
}
}
Use this one to dismiss keyboard :-
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)