iad won't move content to fit in (it covers the content) - ios

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

Related

iOS - keep layout in landscape but change controls

I'm done with the auto-layout stuff in my iOS universal App, and it's working perfectly in portrait. However, I want the user to be able to rotate the device and play the game in landscape mode. The problem I'm facing is that I don't want the layout to change at all, and only change the controls of the game (sliding up the screen should make the player go up in both orientations).
Thing is, I don't know how to prevent orientation from changing the layout and at the same time be able to change behaviour based on the orientation. Do you guys have any idea how I could manage that?
Did found a way to do, for future reference, when an orientation is disabled, we still can access device orientation (and not interface orientation), and register a notification to act upon change.
class ViewController: UIViewController {
var currentOrientation = 0
override func viewDidLoad() {
super.viewDidLoad()
// Register for notification about device orientation change
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceDidRotate(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
// Remove observer on window disappears
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
if UIDevice.current.isGeneratingDeviceOrientationNotifications {
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
}
// That part gets fired on orientation change, and I ignore states 0 - 5 - 6, respectively Unknown, flat up facing and down facing.
func deviceDidRotate(notification: NSNotification) {
if (UIDevice.current.orientation.rawValue < 5 && UIDevice.current.orientation.rawValue > 0) {
self.currentOrientation = UIDevice.current.orientation.rawValue
}
}
}

How can Upside Down orientation be continually detected on an iPhone in viewWillTransitionToSize:coordinator: on iOS 9?

I’m handling rotation changes in viewWillTransitionToSize:coordinator: on an iPhone with iOS 9.1 and I’m able to successfully detect Upside Down orientation if the device is turned upside down, from right side up, after starting the app for the first time. Further rotations to Upside Down do not result in further detections of the Upside Down orientation.
I’m using the following code in a view controller:
override func viewWillTransitionToSize(size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
I have all orientations checked off in the General tab of the Xcode project.
I followed the suggestion in the accepted answer for Rotation behaving differently on iOS6 and implemented my own custom navigation controller.
class MyNavigationController: UINavigationController {
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
}
That did not result in successful continual detection of Upside Down in viewWillTransitionToSize:coordinator:.
Adding the same methods to my view controller also did not change the results:
class MyViewController: UIViewController {
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
Setting up a separate observer using
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationDidChange:", name:UIDeviceOrientationDidChangeNotification, object: nil)
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
also does not continually report the Upside Down orientation when I check the bounds with:
UIScreen.mainScreen().bounds
Instead, the bounds are reported as being the same as for landscape.
Ideally, I would like to have Upside Down be handled by viewWillTransitionToSize:coordinator: but I’m open to workarounds, too.

shouldAutorotate -> false, still simulator rotates

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
}

Placing iAd and Admob banner is causing an issue

I've been trying to do the following with iAd and Admob banner:
First i placed iAd over Admob banner in storyboard and I've added all the needed constraints, if iAd fail to receive i will show Admob banner if i received iAd i am gonna hide Admob and then show iAd banner..etc.
The issue doesn't happen all the time, while admob banner is showing iAd comes and push the admob banner to the top without hiding it. but in the code it should hide it.this issue happens after hiding and showing, hiding and showing many times..
Please check the following screenshot.
ViewController code:
#IBOutlet weak var Gbanner: GADBannerView!
#IBOutlet weak var AbannerView: ADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
AbannerView.delegate = self
self.canDisplayBannerAds = true
showadmob()
// Do any additional setup after loading the view, typically from a nib.
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("didFailToReceiveAdWithError")
AbannerView.hidden = true
Gbanner.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("bannerViewActionDidFinish")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("bannerViewDidLoadAd")
Gbanner.hidden = true
AbannerView.hidden = false
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
println("bannerViewWillLoadAd")
}
func showadmob(){
self.Gbanner.adUnitID = "somethingelsehere"
self.Gbanner.rootViewController = self
var request: GADRequest = GADRequest()
self.Gbanner.loadRequest(request)
}
Download project here : https://yadi.sk/d/1VcjfJG9ixNZg
If you're implementing your own ADBannerView then you need to remove self.canDisplayBannerAds = true from your viewDidLoad.
self.canDisplayBannerAds = true can be used for a no hassle way of implementing iAd banners in your application. This will create an ADBannerView for you and show or hide the ADBannerView depending on whether it receives an ad or not from the iAd network.
You either implement your own ADBannerView or use self.canDisplayBannerAds = true, not both.

iAd banner changes the frame.size of my SKScene

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:

Resources