The first time a user clicks on an iAd banner, it displays correctly and then go back to my game.
The second click actually makes the ad behave as if it was in landscape mode:
So the right side of the view is aligned to the real right side of the tablet, as judged by the "nodes and fps" indicator.
But the dimension of the view is now 1024*768 instead of 768*1024.
Therefore, the center of the scene is almost at the left edge of the iPad!
I tried changing it programmatically in the delegate for bannerAds, without success. Indeed, the size value actually changes, but not my screen!
var currentOrientation:UIDeviceOrientation=UIDeviceOrientation.Portrait
var oldFrameSize:CGSize?
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
//pause game here
currentOrientation=UIDevice.currentDevice().orientation
oldFrameSize=self.frame.size
println("Leave the application to the Ad")
return true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
UIDevice.currentDevice().setValue(currentOrientation.rawValue, forKey: "orientation")
switch(currentOrientation){
case UIDeviceOrientation.Portrait:
self.view?.window?.rootViewController?.setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "interfaceOrientation")
break
case UIDeviceOrientation.PortraitUpsideDown:
self.view?.window?.rootViewController?.setValue(UIInterfaceOrientation.PortraitUpsideDown.rawValue, forKey: "interfaceOrientation")
break
default: break;
}
self.size=oldFrameSize!
self.view?.clipsToBounds=true
println(frame.size.width)
println(oldFrameSize!.width)
println(size.width)
println("Close the Ad")
}
EDIT: here are snapshot of the differences between the first Ad display, and the second Ad display:
Related
I want to know how to rotate a device even when a user turns on the portrait orientation lock in iPhone.
Like the amazon prime video app, normally I see the app is in portrait mode, but when I start watching a movie, the app changes its rotation to horizontal even when I turn on the portrait orientation lock.
When I googled it, I only found this article, but using CoreMotion is the only way to achieve what I want to do??
I was using the following code to rotate the device from this article,
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
But the app doesn't rotate from portrait to landscape automatically (from landscape to portrait works...), but when I rotate the device from portrait to landscape by my hand, the app's view rotates.
So, how can I make the device rotation to landscape when I'm into a specific view controller? (like the prime video app automatically turns the orientation to landscape when I start watching movie)
in AppDelegate following variable to lock all the controllers with given orientation
var orientationLock = UIInterfaceOrientationMask.portrait
Now implement following method in AppDelegate
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
Write these methods in separate Helper file
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: LocalizedKey.orientation.string)
}
Call this method in your specific controller's viewWillAppear()
override func viewWillAppear(_ animated: Bool) {
Helpers.lockOrientation(UIInterfaceOrientationMask.landscape,
andRotateTo: UIInterfaceOrientation.landscapeLeft)
}
NOTE: LocalizedKey.orientation.rawValue is string in separate file which have the value of "orientation"
In Xcode 11 beta 7, I am having issues with my SKScene, my GameScene does not fill the entire screen of the iPad simulator. This is true for all iPad simulators. On my physical iPad the Game Scene is as intended, but I worry this may not be true of all iPad's. On all iPhone simulators and on my iPhone, the Game Scene is also displayed as intended.
I have two SKScenes, one is the Main Menu screen which fills the entire screen, but my Game Scene does not, when I load the Game Scene it is square and the Main Menu screen is visible underneath, like so:
The following is the code for my GameViewController, which is practically identical to my MainMenuViewController except all instances of "Game" are "MainMenu":
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities
sceneNode.graphs = scene.graphs
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
I have tried changing the line sceneNode.scaleMode = .aspectFill to sceneNode.scaleMode = .aspectFit, but that produces the following:
So, how do I make my Game Scene, the red area, fill the entire iPad's screen?
Typical that after setting a bounty I find the answer.
The issue was in my storyboard file, not my code:
Originally, I had this in the properties of my view after the segue:
And changing it to this made the SKView fill the entire screen:
Clearly at some point Xcode changed so that 'Automatic' no longer filled the screen for whatever reason.
Edit: For more clarification, when you open the storyboard file, click/select the view controller that you want to adjust, under the Attributes Inspector, there is an option for Presentation where you can select the "Full Screen" attribute.
I'm working on an app for both iphone/ipad all sizes. I've used the storyboard to place all the elements on screen: labels, buttons and images. Everything works perfectly. I want to place an iad at the bottom of the screen. So I places an iad from the object library and constrained it to the bottom of the screen, without constraining it to the other objects on the view. I've read that the iad has already the ability to push all content (resize) for it to fit on the screen. However when I run my app on the simulator, 6 out of 10 times the iad pushes the content up to make space for it to display. The other 4 times the iad covers the bottom label and button. It happens on all the simulator devices (5, 5s, 6, 6s, 6+ 6+s, ipad 2, ipad pro, ipad air, ipad air2, ipad retina.)
I'm using the following script to display the iad:
#IBOutlet weak var adBannerView: ADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.adBannerView?.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.adBannerView?.hidden = true
}
Does anyone know or have an Idea on how to fix the problem I'm having?
Also in the apple manual it says that if iad fails to load it should be pushed offscreen. Do I need to write code to do this or will it do it automatically by its self?
iad pushes content to fit in, screen shot
iad covers the content, screen shot
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
}
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