iAd shared for UIViews - share

I am using an iAd for sharing among views, but there are two strange issues which I have been browsing over the internet for solution without finding clue, therefore I post my question and code here for brain storming.
.h:
#interface AppDelegate : NSObject {
UIWindow *window;
ADBannerView *adBanner;
appViewController *viewControllerApp;
}
.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
adBanner = [[ADBannerView alloc] initWithFrame:CGRectZero];
adBanner.currentContentSizeIdentifier =ADBannerContentSizeIdentifierLandscape;
adBanner.delegate = self;
adBanner.backgroundColor = [UIColor whiteColor];
adBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
//----------------------------------------------------------------------------------
[window addSubview:viewControllerApp.view];
[window makeKeyAndVisible];
return YES;
}
appViewController.m
-(void)viewWillAppear:(BOOL)animated
{
//#ifdef LITE_VERSION
[SharedAdBannerView setFrame:CGRectMake(0, 40, 0, 0)];
[self.view addSubview:SharedAdBannerView];
//#ifdef
}
somewhere in the appViewController.m:
{....
playGameView = [[PlayGameViewController alloc] initWithNibName:#"PlayGameViewController"
bundle:nil];
[self.view insertSubview:playGameView.view atIndex:10];
}
So, there are two questions:
1. when the App is running, the iAd isn't displayed on the right position (shall be
0,40,0,0) but it doesn't show up unless I changed the position to (0,180,0,0), but it
actually wasn't on the 0,180. it looks like on 0,40.
When I move to 2nd view (PlayGameView), then click on the iAd, the iAd shows a full
screen View, then I close the iAd, the screen of PlayGameView was gone! it was replaced by
the rootView!! how can I close the iAd with the PlayGameView stays?
I don't see problem at rootView when the iAd is clicked and closed.. anyone can help will be
highly highly appreciated!
BR
Georg

ok, now I have finally found the problem, I replaced this
[self presentModalViewController:playGameView animated:NO];
from
[self.view insertSubview:playGameView.view atIndex:10];
then it work perfectlly.
now there is only one remain issue: position of iAd banner view

Related

ios5.1 adding a UIView on top of iphone statusbar

I am trying to add a view on top of the statusbar. I have been following this SO post: Adding view on StatusBar in iPhone
For some reason, when I create the window and set Hidden to NO, the view does not appear to show up on top of the statusbar. Does this implementation still work in ios5.1?
Thanks!
This is my custom UIWindow class:
#implementation StatusBarOverlay
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Place the window on the correct level and position
self.hidden = NO;
self.windowLevel = UIWindowLevelStatusBar+1.0f;
self.frame = [[UIApplication sharedApplication] statusBarFrame];
// Create an image view with an image to make it look like a status bar.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
backgroundImageView.image = [UIImage imageNamed:#"bar_0.png"];
backgroundImageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"bar_1.png"],
[UIImage imageNamed:#"bar_2.png"],
[UIImage imageNamed:#"bar_3.png"],
nil];
backgroundImageView.animationDuration = 0.8;
[backgroundImageView startAnimating];
[self addSubview:backgroundImageView];
}
return self;
}
In my viewcontroller, I created the new window and called this in viewDidLoad:
StatusBarOverlay *overlayWindow = [[StatusBarOverlay alloc] initWithFrame:CGRectZero];
[overlayWindow makeKeyAndVisible];
However, the view still doesn't show up. Any idea as to why?
set your application's window level to UIWindowLevelStatusBar:
self.window.windowLevel = UIWindowLevelStatusBar;
and then add your own view to application window anywhere:
[[[UIApplication sharedApplication]delegate].window addSubview:yourview];
this problem came to me recently, and I just solved it through this way
You can create a new UIWindow and add your view to that. Set the windows frame to [[UIApplication sharedApplication] statusBarFrame] and call makeKeyAndVisible to make it visible.
In the end, I subclassed UIWindow and made that the primary UIWindow in the application AppDelegate. I added any custom view and set the window level to UIWindowLevelStatusBar to display on top of the status bar.

Two switches in a window on iPhone

I've just started learning how to develop an iPhone app.
I'm trying to make an app with two switches. I made two classes (Switch1 & Switch2).
First, I tested the app with one switch (Switch1), and the app worked. But when I made the second class (Switch2) and I Build/Run the app, the first switch (Switch1) disappeared, and what I saw just the second switch (Switch2).
After that I made the background of the (Switch1 & Switch2) celarColor, I could see both of switches. However, the first switch (Switch1) can't be switched.
so I think my problem is how to make both switches (Switch1 & Switch2) visible and working at the same time in the "window"
The question (could be stupid): What can I make them visible and working at the same time?
I think the problem in the following code: This is from the AppDelegate
UIScreen *s1 = [UIScreen mainScreen];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
[window addSubview: view1];
[window makeKeyAndVisible];
UIScreen *s2 = [UIScreen mainScreen];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s2.bounds];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
Here is the Switch1.h
#import
#interface Switch1 : UIView {
UISwitch *mySwitch1;
}
#property (nonatomic, retain) IBOutlet UISwitch *mySwitch1;
#end
Here is the Switch1.m
#import "Switch1.h"
#implementation Switch1
#synthesize mySwitch1;
- (id) initWithFrame: (CGRect) frame {
if ((self = [super initWithFrame: frame])) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
mySwitch1 = [[UISwitch alloc] initWithFrame: CGRectZero];
if (mySwitch1 == nil) {
[self release];
return nil;
}
mySwitch1.on = NO; //the default
[mySwitch1 addTarget: [UIApplication sharedApplication].delegate
action: #selector(valueChanged:)
forControlEvents: UIControlEventValueChanged
];
CGRect b1 = self.bounds;
mySwitch1.transform = CGAffineTransformMakeScale(2, 2);
mySwitch1.center = CGPointMake(
b1.origin.x + b1.size.width / 2,
b1.origin.y + b1.size.height / 2
);
[self addSubview: mySwitch1];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect: (CGRect) rect {
// Drawing code
}
*/
- (void) dealloc {
[mySwitch1 release];
[super dealloc];
}
#end
You might want to configure a view controller with a view and then toss your two switches on that first. Do you understand the MVC patterns here: https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
and here's the view controller guide: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html.
When you're initializing with UIScreen, you're making both switches the same size (the window size) and thus switch 2 is over switch 1 since it's initialized second.
So you are adding one screen (s2) on top of another screen (s1) and therefore you are not able to access s1. You need to make the size of s2 and s1 smaller so that they do not take the whole screen size.
Also by saying makeKeyAndVisible you make the window visible and able to accept user interaction. No need to say that twice.
You're resetting the window when you call
window = [[UIWindow alloc] initWithFrame: s2.bounds];
s1 isn't there anymore because you've created a new window over it. You could just do
UIScreen *s1 = [UIScreen mainScreen];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
[window addSubview: view1];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
If you're just starting, definitely check out this tutorial on iPhone dev. It shows how to use UIViewController, UIView, and a lot of the supplied classes for the iPhone like UITableView and UIImageView.

Default.png shows, but not for long

LOTS of Default.png Q's, but I didn't read one like this: I see Default.png, but then the screen goes blank, but no error messages, no abort. What am I missing?
SDK 4.3, iPhone only, no nib, no schemes, no orientation other than portrait up/down. Nav controller, 4 tabs.
App ABC works fine.
Clone XYZ doesn’t.
info.plist seems identical, including the half dozen icon files; neither app has an entry that contains “MainWindow.” Analyze and both generate “Build succeeded.” Run and both display “Attaching to process blah-blah-blah.” Any combo of reboot and/or restart Mac/XCode/simulator/device doesn’t do a dang thing. There is one difference, though: the drop-down for ABC shows the full device name, two 4.2’s and two 4.3’s; XYZ shows only “IOS Device” and the two 4.3’s. ABC shows all the NSLogs. XYZ shows none of them. And in both cases, the first NSLog is right after
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Clean all targets made no difference. I’m thinking, “Clone NOT.”
XJones, here's the code you requested. I've whacked it down as much as I could.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"app delegate CGRect");
CGRect frameMain = [UIScreen mainScreen].applicationFrame;
glbl_height = frameMain.size.height;
glbl_width = frameMain.size.width;
NSLog(#"app delegate background");
UIColor *blueGreenBackground = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Default.png"]];
self.window.backgroundColor = blueGreenBackground;
[blueGreenBackground release];
NSLog(#"app delegate 3");
Method01_vc *method01VC = [[[Method01_vc alloc] init] autorelease];
// more controllers
method01VC.title = NSLocalizedString(#"Title 1, Method 1",#"(title 1)");
// more titles
UINavigationController *method01Nav = [[[UINavigationController alloc] initWithRootViewController:method01VC] autorelease];
// more controllers
NSLog(#"app delegate prior to tabBar init");
_tabBarController = [[UITabBarController alloc] init];
NSArray *sections = [NSArray arrayWithObjects:method01Nav, method02Nav, method03Nav, method04Nav, nil];
[_tabBarController setViewControllers:sections];
CGRect frame = CGRectMake(0.0, 0.0, 320, 48); // 48
UIView *tabBarView = [[UIView alloc] initWithFrame:frame];
[tabBarView setBackgroundColor:[UIColor colorWithRed:0.07 green:0.14 blue:0.04 alpha:0.8]];
NSArray *tabs = _tabBarController.viewControllers;
UIViewController *tab1 = [tabs objectAtIndex:0];
tab1.tabBarItem.image = [UIImage imageNamed:#"tabIcon01.png"];
tab1.tabBarItem.title = #"";
UIViewController *tab2 = [tabs objectAtIndex:1];
tab2.tabBarItem.image = [UIImage imageNamed:#"tabIcon02.png"];
tab2.tabBarItem.title = #"";
NSLog(#"app delegate prior to tabBar insert");
[[_tabBarController tabBar] insertSubview:tabBarView atIndex:0];
tabBarView.autoresizesSubviews = YES;
[tabBarView release];
_tabBarController.delegate = self;
[self.window addSubview:_tabBarController.view];
// self.tabBarController.selectedIndex = 1; ---- makes no difference
// self.window.rootViewController = self.tabBarController; --- ditto
NSLog(#"app delegate prior to makeKeyAndVisible");
[self.window makeKeyAndVisible];
NSLog(#"app delegate, about to return YES");
return YES;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSLog(#"Method01 CGRect");
CGRect frameMain = [UIScreen mainScreen].applicationFrame;
UIView *view = [[UIView alloc] initWithFrame:frameMain];
self.view = view;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
After the above, all the usual labels and text fields follow.
Thanks again for taking the time to slog through it.
I've got my hopes set on a "Duh!"
Default.png is only displayed while iOS is launching your app. B/c your app doesn't do anything it launches very quickly so Default.png goes away and is replaced by the UI of your app. Either you are not adding a view to your apps window or the view you added displays as blank.
[EDIT 1: need to create a window]
Looked at your code and if you are not using IB you need to initialize the window in the app delegate before you add the tab bar controller's view to it.
self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
Here's a link to a blog that show's everything you need to do if not using IB:
http://code-dojo.blogspot.com/2009/11/build-iphone-application-without.html

iphone, addSubview show view too high up, how do I move to down?

I'm using addSubView twice in my app and both times it adds the view too high up the view, its off the screen at the top. See below... I have load the views like this as I nothing else works, cause me problems.
I just don't understand why they are showing off the screen?
What do I need to do to fix this ?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:rootController.view];
[window makeKeyAndVisible];
lvc = [[GettingStartedViewController alloc]
initWithNibName:#"GettingStartedView" bundle:nil];
[window addSubview:lvc.view];
return YES;
}
And in my GettingStartedView ...
- (IBAction) showHelp:(id)inSender {
theController = [[HelpViewController alloc] initWithNibName:#"HelpView"
bundle:nil onPage:HelpPageGettingStarted];
[self.view addSubview:theController.view];
}
Set the location:
rootController.view.frame = CGRectMake(0, 20, 320, 460);
Check the wantsFullScreenLayout property of your root view controller, make sure it's not set.

Simple ViewController / View, remove white bar?

I am just looking at setting up a simple viewController programatically, I have a ViewController.xib file that I have set the background color to RED in interface builder. I have also added the following to my AppDelegate.m
#implementation syntax_MapViewAppDelegate
#synthesize window;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[MapViewController alloc] init];
[window addSubview:[viewController view]];
[window makeKeyAndVisible];
return YES;
}
-(void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
When I run the code it does what I expect apart from the white bar at the bottom of the screen, can anyone give me any pointers in how to remove this? I have a feeling I might need to position the view within the window, but I am not sure how?
cheers Gary
Please set your view frame.
viewController.view.frame = CGRectMake(0.0,20.0,320.0,460.0);
Now the view cover your full screen with Red color.
You can also set it from interface builder as well.
Something like this might save your problem:
[[viewController view] setFrame:CGRectMake(0, 20, 320, 460)];
Set your view to resize dynamically using the autoresizingMask or set it's frame explicitly.