I tried this code for scanning barcode:
https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode
but it have a bug, when i change orientation of device, camera always has a Protrait orientation...
Any ideas how to fix this ?
I tried to change orientations in this method:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
But it not helps.
For a camera to change the orientation you don't want to use that function. Do this instead...
previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
What this does is when the camera layer is created it sets the orientation.
Related
I am able to take MKMapView snapshot in portrait mode, but not in landscape mode. I automatically rotate my UIView to landscape using the functions given below. I can see the map-view as rotated to landscape. But, when I take snapshot using MKMapSnapshotter, I get an image with map in portrait mode again.
override var shouldAutorotate: Bool {
return true
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
My hunch is it has something to do with setting MKMapSnapshotOptions to landscape, but I am not sure how to do that.
let mapSnapshotOptions = MKMapSnapshotOptions()
mapSnapshotOptions.region = mapView.region
mapSnapshotOptions.scale = UIScreen.main.scale
mapSnapshotOptions.size = mapView.frame.size
I expect that above code should have handled it since map-view is already rotated to landscape, but it does not.
mapView.frame.size = (375.0, 647.0)
I observe the same value of mapView frame for both cases: portrait and landscape.
I tried using CLLocationManagerDelegate. But to no gain. The below function gets called when snapshot options are already set.
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
// heading is updated for delegates only after any transition has ended
// therefore, snapshot properties are set already by the time program reaches this function
mapView.camera.heading = newHeading.magneticHeading
mapView.setCamera(mapView.camera, animated: true)
}
Please suggest what more can be done?
I'm working on an app that is to be in portrait mode when it is launched. After that, I add an SDK for some purpose to my project. In the process of the integrating the SDK needs both Portrait and Landscape Right checked in the General pane. So I set it as given in the SDK integration manual.
and after that when I run my project in a device and rotate it to right the app device orientation is changing. How can I set the orientation is fixed without disturbing the SDK.
if you're having trouble to set orientation right after the app launches then you might need to stop orientation for each view controller or one of it's base navigation controller
Swift 3:
class YourBaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}}
Use YourBaseNavigationController as your navigation controller.Basically all view controller within this navigation controller will be force to support portrait mode only.
unmark the checks Landscape left and landscape right
You need to disable orientation through code.
Add this code in your RootViewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);//set 'return NO'; for stop orientation
}
Swift :
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
How can I just rotate the buttons when my device is in portrait or landscape mode? I know something like that:
button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
But where I have to call it up in my code?
I don't want to set my device in landscape mode, I just want to rotate the icons when it should be in landscape mode.
Thanks in advance!
You should override func
viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
And call transform there
Don't forget to assign CGAffineTransformIdentity when rotated back
More about rotation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:
The best way to do this is viewDidLoad add this line of code bellow:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotate), name: UIDeviceOrientationDidChangeNotification, object: nil)
and then in function rotate in case of device orientation do some code like this:
func rotate(){
if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
//your code here in landscape mode
}
if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
//your code in portrait mode
}
}
this solution is simple and really easy to do.
I guess you know how to "rotate" the buttons already, so I'll just tell you how to know that the device has rotated.
In a view controller, override willRotateToInterfaceOrientation:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
In the method body, you can check the value of toInterfaceOrientation to know which orientation the device is rotating to. For example:
switch toInterfaceOrientation {
case Portrait:
// some code here...
default:
break
}
I want to change the orientation of my app. The only thing I found is:
self.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT)
but it says the UIA_Device_Orientation_Portrait could not be found.
Can anyone tell me why (perhaps I have to import something) or how else i could set the Orientation to Portrai mode?
thanks
If you wanted to rotate manually, do like this:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")
If automatically, do like this:
// ViewController.swift
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .All
}
And If your view controller is in a navigation/tabbar controller, please set that navigation/tabbar controller.
UPDATE:
This is how to control autorotate
override func shouldAutorotate() -> Bool {
return false // adjust this
}
Make sure you have your main plist adjusted to allow multiple orientations. You'll find this under the Supported interface orientations tag in your plist.
I want to Restrict one page in portrait orientation while whole application in both landscape and portrait orientation how to achieve this functionality. I have tried old code from stack over flow but it's not working.
If any one solved such problem then please help me thanks in advance.
Add this snippet to the ViewController which should not rotate:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}