How to Change Uiwebview Orientation Programmatically in Swift

How to change UIWebView orientation programmatically in Swift

In your viewDidAppear add the following:

override func viewDidAppear(animated: Bool) {
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

Swift 3.0:

override func viewDidAppear(_ animated: Bool) {
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}

Just choose your orientation and it will be set to it when the WebView is shown.

uiwebview not resizing after changing orientation to landscape

Try setting constraints on the UIWebView. When the View rotated, the space constraints should automatically adjust.

In the storyboard, look for the menu icons in the lower right. Select the auto layout icon (looks like a tie fighter), and then select either 'Add Missing Constraints' or 'Reset to Suggested Constraints'.
Sample Image

Allowing orientation / rotation change only on a UIWebView

I ran into a similar problem when I wanted to show an image fullscreen in landscape mode, and it's default position in portrait mode. Since my app contained a TabBarController with each of the tabs displaying a navigation controller, I had to move things around myself using "willAnimateRotationToInterfaceOrientation" for the view that contained the image.

In my app, the tab bar controller will display in all orientations except portrait upside down. If you lock the tab bar controller to one orientation, i believe you lock all subsequent views within the tab bar as well. Essentially i hid the status bar, the navigation bar and the tab bar whilst moving the navigation bar up out of view, and the tab bar down out of view, and resizing the content in the middle. Here's an example of what i did, supposing that you have self.WebView (that would have been my image, for example):

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {

float navBarHeight = self.navigationController.navigationBar.frame.size.height;
float tabBarHeight = ((UITabBarController *)self.navigationController.parentViewController).tabBar.frame.size.height;

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
float statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.navigationController.navigationBar.hidden = YES;
((UITabBarController *)self.navigationController.parentViewController).tabBar.hidden = YES;

// TabBarController adjustments
self.navigationController.parentViewController.view.bounds = CGRectMake(0, -tabBarHeight/2, 480, 320 + tabBarHeight);

// Adjust view
self.view.frame = CGRectMake(0, -statusBarHeight - navBarHeight, 480, 320);

// Adjust web view
self.WebView.frame = CGRectMake(26, 0, 426.6667, 320);
}

if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
float statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
self.navigationController.navigationBar.hidden = NO;
((UITabBarController *)self.navigationController.parentViewController).tabBar.hidden = NO;

// TabBarController adjustments
self.navigationController.parentViewController.view.bounds = CGRectMake(0, 0, 320, 480);

// NavigationController adjustments
self.navigationController.navigationBar.frame = CGRectMake(0, statusBarHeight, 320, navBarHeight);

// Adjust view
self.view.frame = CGRectMake(0, statusBarHeight + navBarHeight, 320, 480 - statusBarHeight - navBarHeight - tabBarHeight);

// Adjust web view
self.WebView.frame = CGRectMake(0, 0, 320, 240);
}
}

I believe by resizing your webview it should automatically fit the content within appropriately without having to "refresh" per se.

How to detect rotation for a programatically generated UIView

I think you have two options:

  1. You put the view in a custom UIViewController and override the shouldAutorotateToInterfaceOrientation: method to return YES for all the orientations you want to support. The view controller will automatically rotate and resize the content (your view). You just have to make sure to have the correct autoresizeMask (or constrains, if you're using autolayout).
  2. You directly observe changes to the devices orientation with the accelerometer. Then you can adjust your view when needed.

It's really the first approach you should choose if you can.

Swift - Keep UIImageView in the same position on rotate

I think these things are always easier to do without using autolayout. To do this, I recomend using viewDidLayoutSubviews(). Here is my code:

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

webView.frame = view.bounds

let screen = UIScreen.mainScreen().fixedCoordinateSpace

//These values will give a rect half the size of the screen and centered.
let width = screen.bounds.width / 2
let height = screen.bounds.height / 2
let x = (screen.bounds.width - width) / 2
let y = (screen.bounds.height - height) / 2
let absoluteRect = CGRect(x: x, y: y, width: width, height: height)

let stampRect = screen.convertRect(absoluteRect, toCoordinateSpace: webView)
stampView.frame = stampRect

//Change the orientation of the image
switch UIDevice.currentDevice().orientation {
case .LandscapeLeft:
stampView.image = UIImage(CGImage:originalImage.CGImage!, scale:originalImage.scale, orientation: UIImageOrientation.Left)
break
case .LandscapeRight:
stampView.image = UIImage(CGImage:originalImage.CGImage!, scale:originalImage.scale, orientation: UIImageOrientation.Right)
break
default:
stampView.image = UIImage(CGImage:originalImage.CGImage!, scale:originalImage.scale, orientation: UIImageOrientation.Up)
break
}
}

I am doing several things here...First I set the webViewFrame. Next, it is helpful here to define an absolute coordinate system relative to your screen. When the phone orientation changes, the values of screen will not change (allowing you to keep your imageView in the same place.) Next I define the desired frame for the stampView, then convert the absolute frame into its equivalent inside your scrollView (it's superView) and assign it to the stampView. Finally, if you want the image in your stampView to always be oriented correctly, you need to change the image orientation. CGAffineTransformMakeRotation only works if your view is square, but we can make it more general by actually changing the imageOrientation within the stampView. This requires a property for the original image:

let originalImage = UIImage(named: "image_name")!

Finally, because viewWillLayoutSubViews() is not called for landscape to landscape transitions, do the following:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
view.setNeedsLayout()
}

This code doesn't make for the prettiest transitions, it should help with your layout problems.

Swift Programmatically zoom out of UIWebView

Try adding this in webViewDidFinishLoad method.

let contentSize:CGSize = theWebView.scrollView.contentSize
let viewSize:CGSize = self.view.bounds.size

let rw:float = viewSize.width / contentSize.width

theWebView.scrollView.minimumZoomScale = rw
theWebView.scrollView.maximumZoomScale = rw
theWebView.scrollView.zoomScale = rw


Related Topics



Leave a reply



Submit