I want the user to rotate the device but change nothing in the view (do not rotate the view 180 degrees). All I want is to println a sentence.
Here is my code yet - (it println the sentence but also rotate the device's view. In this example it was to the left but it does't really matter to what side):
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft) {
println("Device has been rotated.")
}
return true
}
Change
return true
to
return false
Related
Is possible to turn off the backswipe for certain part of the screen? In particular, I was thinking of using this method:
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
return false;
}
I was thinking to recognize what part of the screen the user has pressed on, if it is within a certain interval, I would return true, else false. Not sure how to pickup where the user has pressed.
I assume you are talking about the functionality provided by the UINavigationController.
First your class should conform to the UIGestureRecognizerProtocol something like this:
class MyController: UIViewController, UIGestureRecognizerProtocol
Then you need to register to become the delegate for the navigation controller so probably in your viewDidLoad or viewWillAppear (depending on needs) you have this:
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
Then you can use the following to check where in the view the gesture start:
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let location = gestureRecognizer.location(in: self.view)
if location.y > 500 {
return false
}
return true
}
Obviously this is just a test case where it ignores the gesture when it starts above 500 in the vertical and you can change that to whatever you want.
I'm trying to change orientation with just one view, the rest are anchored to Portrait. I've set a method in my AppDelegate as below
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if globalVariables.gIsDosageView == "Y" {
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
return UIInterfaceOrientationMask.all;
} else {
return UIInterfaceOrientationMask.portrait;
}
} else {
return UIInterfaceOrientationMask.portrait;
}
}
The global variable gIsDosageView is set to "Y" when the Dosage view is selected. I've created two separate views, portraitView (375x812) and landscapeView (812x375). I've used the viewWillTransition method to catch each change in orientation as below
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if size.width < 400 {
self.userImg.isHidden = false
self.deviceImg.isHidden = false
self.portraitView.isHidden = false
self.landscapeView.isHidden = true
}
else {
self.userImg.isHidden = true
self.deviceImg.isHidden = true
self.portraitView.isHidden = true
self.landscapeView.isHidden = false
}
}
When I go from the main screen to Dosage it displays the correct screen and will toggle between both orientation views without issue. However, if I go to another screen and come back to the Dosage screen it only shows the screen that was first loaded in both orientation screens. I've stepped through the code and it hides the right screens but this is not reflected in the resulting view. If I select the screen in portrait first, it will toggle successfully between portrait and landscape but if I go to the next screen and return to Dosage, it will only show the Portrait screen regardless of orientation and appears to ignore the code in viewWillTransition().
Why is this and what have I missed?
viewWillTransition is only called when the device is rotated. When you switch between open views without rotating, it won't get called, so won't set the view up how you want it. Try putting the test in viewWillAppear instead.
I was trying to figure how to work with the iPhone X notch but I can't understand how to make the home bar not activate when a user swipes from the bottom.
I used the following code as suggested in this post and it does work for the top swipe but it doesn't work for the bottom swipe.
Am I doing something wrong?
override func preferredScreenEdgesDeferringSystemGestures() -> UIRectEdge {
return .all
}
This bit works perfectly. Swipe twice or "fast" to show the home bar.
override var prefersStatusBarHidden: Bool {
if #available(iOS 11.0, *) {
return super.prefersStatusBarHidden
} else {
return true
}
}
override func preferredScreenEdgesDeferringSystemGestures() -> UIRectEdge {
return [.all]
}
A question i have is--Will apple allow it if and when I send it in my code when i submit my app to the app store?
I have a scroll view with 2 views in it: a UIImagePicker (Snapchat-style camera view), and a UITableView.
The scroll view is inside of a navigation controller that the main viewController pushes to. I want the status bar and everything on it (time, battery, wifi, etc.) to be hidden on the camera view, but when you scroll to the right to the tableView, the status bar contents show back up, whether they do some kind of cool stretch animation as you scroll (would be awesome to figure that out) or any other solution possible.
Hopefully I worded this well enough for you to understand.
Solution I found (More of a workaround)
declare a boolean called hidden.
Then I overrode these methods:
func scrollViewDidScroll(scrollView: UIScrollView){
let xOffset = scrollView.contentOffset.x;
if(xOffset > scrollView.contentSize.width/4)
{
if hidden == true {
print("\nShow status bar\n")
hidden = false
UIView.animateWithDuration(0.3, animations: {
self.setNeedsStatusBarAppearanceUpdate()
})
}
} else
{
print("\nHide Status Bar\n")
hidden = true
UIView.animateWithDuration(0.2, animations: {
self.setNeedsStatusBarAppearanceUpdate()
})
}
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
if hidden == false {
return UIStatusBarAnimation.Fade
} else {
return UIStatusBarAnimation.Slide
}
}
override func prefersStatusBarHidden() -> Bool {
print("\nstatus Bar Changed to hidden = \(hidden)\n")
return hidden
}
It fades the status bar in once you've at least scrolled half way, and slides the status bar back up once you've gone back half way again.
Have you tried calling
UIApplication.sharedApplication().setStatusBarHidden(hidden: Bool, withAnimation: UIStatusBarAnimation)
In appropriate timing (which is not recommended because you'll have to set it back to what it was when leaving the view, just so you know)
Or the override method
override func prefersStatusBarHidden() -> Bool {
code
}
in your controller?
I'm making an app which supports landscape orientations only when playing a video. Otherwise, all the scenes support only portrait orientation. I've checked portrait, landscape left and right in project settings. I've written the following code in the ViewControllers where I want to restrict to only portrait.
override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
return UIInterfaceOrientation.Portrait.rawValue
}
Still, when I press (Command + right or left arrow), the simulator rotates. I don't have a device, so I have to test it in simulator.
Please help! Thank you!
It's the parent navigation controller that decide if its content should rotate or note.
You will need to override UINavigationController and had something like this
override func shouldAutorotate() -> Bool {
return self.topViewController.shouldAutorotae()
}
override func supportedInterfaceOrientations() -> Int {
return self.topViewController.shouldAutorotae()
}
Two things to check. First, check your info.plist file and make sure that you have portrait deleted for both iPhone and iPad. I was having the same issue and it was because I hadn't deleted the iPad "Portrait" options. See here:
Second, the below code can help. However, it may not work if you have a navigation controller.
override var shouldAutorotate: Bool {
return true
}