So I roughly followed this tutorial on how to make an iAd banner not cover a Phonegap app, but had to improvise because it didn't really work. So in my webViewDidFinishLoad in my mainViewController method, here is what I have:
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
adView.frame = CGRectOffset(adView.frame, 0, [[UIScreen mainScreen] bounds].size.height - 70);
adView.delegate = self;
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
[theWebView addSubview:adView];
[self.view bringSubviewToFront:adView];
return [ super webViewDidFinishLoad:theWebView ];
}
adView has been properly initialized and is functioning properly. What breaks this (as in I can't click the banner) is this code in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 70;
self.webView.frame = viewBounds;
}
[super viewWillAppear:animated];
}
I added the 70px offset in order to have the banner not cover the content. Now, if I remove this code, I can click the banner fine. What is wrong?
Silly me. I was adding the subview to theWebView instead of self.view, which made it outside of its boundary and unclickable.
Related
I added a UIScrollView to my main UIViewController but for some reason i can't see anything. I painted the scroll view in grey in order to see the actual size, but all i get is a white screen.
Here is my very simple code:
-(void) viewDidLoad{
[super viewDidLoad];
UIScrollView *scroll;
[scroll setBackgroundColor:[UIColor grayColor]];
CGRect screenRect = [[UIScreen mainScreen] bounds];
_orientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsPortrait(_orientation)){
scroll.showsHorizontalScrollIndicator = YES;
}else{
scroll.showsVerticalScrollIndicator = YES;
}
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(screenRect.origin.x,
screenRect.origin.y,
screenRect.size.width,
screenRect.size.height)];
[self.view addSubview:scroll];
}
The view size might change, i'm working on a changing iOS device size.
Hello I am trying to implement iAds at the bottom of my app When I change size to 3.5" screen, the iAd banner I have at the bottom of screen disappears. When I switch back to 4", the banner is back. How do I lock down the banner to display at bottom of screen regardless of screen size?
Here is my .h file
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
{
ADBannerView *adView;
BOOL bannerIsVisible;
NSInteger HighScoreNumber;
IBOutlet UILabel *HighScore;
}
#property (nonatomic,assign) BOOL bannerIsVisible;
#end
this is .m file
#synthesize bannerIsVisible;
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
//banner is invisible
banner.frame = CGRectOffset(banner.frame, 0, 560);
[UIView commitAnimations];
self.bannerIsVisible = YES;
[banner setAlpha:1];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
//banner is visible and we moved it out of the screen due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -560);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication: (BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction) {
// stop all interactive proccess in the app
// video pause
//audio pause
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
// resume everything
// video
// audio
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:#"HighScore: %li", (long)HighScoreNumber];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
- (void) viewWillDisappear:(BOOL)animated
{
[adView removeFromSuperview];
adView.delegate = nil;
adView = nil;
}
Here is all of my code for iAds I really need help with this issue.Thanks
I know this is old but I had this problem recently, and I found a solution so I will post it here in case someone else stumbles upon it.
I noticed that when you change the phones for some reason the ViewController retained the size specified in the storyboard (in my case it was iPhone 6).
So my fix was in the viewDidLoad method I got the size of the main screen after it loaded, instead of the size of the actual view or frame.
Swift:
var screenSize = UIScreen.mainScreen().bounds
println(screenSize)
var viewSize = self.view.bounds
println(viewSize)
Objective-C:
CGSize sizeOfScreen = [[UIScreen mainScreen] bounds].size;
CGSize sizeOfView = self.view.bounds.size;
and the corresponding output in their respective order:
(0.0, 0.0, 320.0, 568.0)
(0.0, 0.0, 375.0, 667.0)
I testing this on a iPhone5S which has a screen size of 320x568, but when asked for the view's bounds I got 375x667 which is the size of which I had made it in the storyboard.
Hope this helps someone!
Add a #define to know which device uses the App :
#define IS_IPHONE5() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && [UIScreen mainScreen].bounds.size.height == 568)
Next, use this #define to modify the offset :
banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? 560.0f : 420.0f));
and
banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? -560.0f : -420.0f));
Set the right value for iPhone 4" and 3.5"
Set the banner position at viewWillAppear where you can access the self.view.bounds.size, don't forget to add constraints or set autoResizingMask to make it "stick" to the bottom.
I have a scenario in mind, which I tried to implement but to no avail- Put a scrolling view with a text box, all of that within page controller.
Here's what I've done- UIPageViewController->ViewController with data ->UIView ->UIScrollView -> UITextView.
What it does? Well, other than scrolling a bit (not to show all of the text) and showing a weird white bar at the bottom, nothing.
Useful snippets of code- basically I linked my height constraint, and then in code:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.frame.size];
self.theScrollView.frame = CGRectMake(self.theScrollView.frame.origin.x, self.theScrollView.frame.origin.y, 320, 2000);//[_theScrollView sizeThatFits:_theScrollView.frame.size];
_heightOfText.constant = 2000; //sizeThatShouldFitTheContent.height;
//_textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, sizeThatShouldFitTheContent.width, sizeThatShouldFitTheContent.height);
_textView.scrollEnabled = NO;
NSLog(NSStringFromCGRect(self.theScrollView.frame));
self.theScrollView.scrollEnabled = YES;
}
But nope.
Thanks in advance
This solved my issue- apparently I should call a selector...
-(void)viewDidLoad
{
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height + 40);
[self performSelector:#selector(adjustScrollViewContentSize) withObject:nil afterDelay:0.1];
}
-(void)adjustScrollViewContentSize
{
_scrollView.contentSize = CGSizeMake(4000, 4000);
}
I am migrating my application to iOS 7. For handing the status bar issue I have added this code
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
CGRect frame = self.navigationController.view.frame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
frame.origin.y = 20;
}
else
{
frame.origin.x = 20;
}
[self.navigationController.view setFrame:frame];
}
This is working fine in normal case. If I am changing orientation (app supports only landscape orientation) or presenting any view controller and dismissing model view controller my view controller alignment changed. The status bar again overlaps my view controller. This piece of code is not working at all. Please guide me to fix this status bar issue.
Case 2: This is how I am presenting my view controller
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
reader.supportedOrientationsMask = ZBarOrientationMaskLandscape;
else
reader.supportedOrientationsMask = ZBarOrientationMaskPortrait;
[self presentModalViewController:reader animated:YES];
Ref:
Fix for status bar issue in IOS 7
Finally I fixed the status bar over lap issue using the delta value property in xcode5. First I have increased origin - y 20pxl to all the controller used in the Xib (it seams to be working fine only in IOS 7), after that I set the delta value for all the view controller origin -y to -20 it works fine in both iOS 6 and iOS 7.
Steps to do that.
Xcode 5 provide preview option to view the appearance of the xib in different view based on the OS version.
Choose preview option from assistant editor
Click assistant editor
and choose preview option to preview selected view controller in different version.
view controller view preview option.
in preview you can find the toggle option to preview view in different version. In preview u can feel the status bar issue clearly if its not fixed properly by toggle the version.
Three steps to fix the status bar issue:
step 1: Make sure the view target us 7.0 and later in File inspector.
Step 2 : Increase the origin - y with 20 pixel (exactly the size of the status bar) for all the controls added in the view controller.
Step 3 : Set the delta value of origin y to -20 for all the controls then only it will adjust automatically based on the version. Use preview now and feel the differ that the controls automatically adjust because of the delta value.
Once the status bar issue fixed, issue while presenting the model view (ZbarSDk controller) is also fixed automatically.
Preview screen :
I am late for this Answer, but i just want to share what i did, which is basically
the easiest solution
First of all-> Go to your info.plist File and add Status Bar Style->Transparent Black Style(Alpha of 0.5)
Now ,here it Goes:-
Add this code in your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Whatever your code goes here
if(kDeviceiPad){
//adding status bar for IOS7 ipad
if (IS_IOS7) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 1024, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; //change this to match your navigation bar
[self.window.rootViewController.view addSubview:addStatusBar];
}
}
else{
//adding status bar for IOS7 iphone
if (IS_IOS7) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; //You can give your own color pattern
[self.window.rootViewController.view addSubview:addStatusBar];
}
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
self.window.clipsToBounds =YES;
self.window.frame =CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}
[self.window makeKeyAndVisible];
return YES;
}
set the following to info.plist
View controller-based status bar appearance = NO;
To hide status bar in ios7 follow these simple steps :
In Xcode goto "Resources" folder and open "(app name)-Info.plist file".
check for "View controller based status bar appearance" key and set its value "NO"
check for "Status bar is initially hidden" key and set its value "YES"
If the keys are not there then you can add it by selecting "information property list" at top and click + icon
MUCH MUCH MUCH simpler answer:
Align the top of your view to the "top layout guide", but control-dragging "Top Layout Guide" to your view and setting the "vertical" constraint. See this answer for a picture reference.
The way it works is - the "Top Layout Guide" will automagically ajust itself for when the status bar is or is not there, and it will all work - no coding required!
P.S. In this particular example, the background showing through at the bottom should also be resolved by setting an appropriate vertical constraint of the view's bottom, to it's superview, or whatever...
Hear we can do this for all views at once
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Notification for the orientaiton change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
// Window framing changes condition for iOS7 or greater
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
statusBarBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.window.frame.size.width, 20)];//statusBarBackgroundView is normal uiview
statusBarBackgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.730];
[self.window addSubview:statusBarBackgroundView];
self.window.bounds = CGRectMake(0, -20, self.window.frame.size.width, self.window.frame.size.height);
}
// Window framing changes condition for iOS7 or greater
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And While we are using orientation we can add below method in app delegate to set it via orientation.
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
statusBarBackgroundView.hidden = YES;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
int width = [[UIScreen mainScreen] bounds].size.width;
int height = [[UIScreen mainScreen] bounds].size.height;
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
self.window.bounds = CGRectMake(-20,0,width,height);
statusBarBackgroundView.frame = CGRectMake(-20, 0, 20, height);
break;
case UIInterfaceOrientationLandscapeRight:
self.window.bounds = CGRectMake(20,0,width,height);
statusBarBackgroundView.frame = CGRectMake(320, 0, 20, height);
break;
case UIInterfaceOrientationPortraitUpsideDown:
statusBarBackgroundView.frame = CGRectMake(0, 568, width, 20);
self.window.bounds = CGRectMake(0, 20, width, height);
break;
default:
statusBarBackgroundView.frame = CGRectMake(0, -20, width, 20);
self.window.bounds = CGRectMake(0, -20, width, height);
break;
}
statusBarBackgroundView.hidden = NO;
}
}
You should Add below navigation controller category for it
.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface UINavigationController (iOS6fix)
#end
.m
#import "UINavigationController+iOS6fix.h"
#implementation UINavigationController (iOS6fix)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
With Salesforce SDK 2.1 (Cordova 2.3.0) we had to do the following to get the status bar appear on the initial load of the App and coming back from the background (iPhone and iPad):
Contrarily to other solutions posted here, this one seems to survive rotation of the device.
1-Create a category of theSFHybridViewController
#import "SFHybridViewController+Amalto.h"
#implementation SFHybridViewController (Amalto)
- (void)viewWillAppear:(BOOL)animated
{
//Lower screen 20px on ios 7
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = self.view.bounds;
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
}
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = self.view.bounds;
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
}
[super viewDidLoad];
}
#end
2-Add to AppDelegate.m imports
#import "SFHybridViewController+Amalto.h"
3-Inject at the end of of method didFinishLaunchingWithOptions of AppDelegate
//Make the status bar appear
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
4-Add to App-Info.plist the property
View controller-based status bar appearance with value NO
i solved this by using below code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(landScape mode)
if ([UIDevice currentDevice].systemVersion.floatValue>=7) {
CGRect frame = self.window.frame;
frame.size.width -= 20.0f;
frame.origin.x+= 20.0f;
self.window.frame = frame;
}
if(portrait)
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
CGRect frame = self.window.frame;
frame.origin.y += 20.0f;
frame.size.height -= 20.0f;
self.window.frame = frame;
}
return YES;
}
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
if (_kisiOS7)
{
[[UINavigationBar appearance] setBarTintColor:_kColorFromHEX(#"#011C47")];
}
else
{
[[UINavigationBar appearance] setBackgroundColor:_kColorFromHEX(#"#011C47")];
[[UINavigationBar appearance] setTintColor:_kColorFromHEX(#"#011C47")];
}
There are several different ways. One approach is to use .plist file
Add a new key "View controller-based status bar appearance" and set value as "NO".
Add another key "Status bar is initially hidden" and set value as "YES".
This will hide status bar throughout project.
just set the following code in viewWillAppear.
if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
I have an app that I want to only work with in Landscape.
For the first time ever, I'm foregoing IB and trying to set up all my views programmatically, so I'm creating a view and adding a bunch of subviews in loadView method:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.delegate = self;
self.mapView.mapType = kGMSTypeHybrid;
self.mapView.frame = self.view.frame;
[self.view addSubview:self.mapView];
// add the toolbar
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
toolbar.barStyle = UIBarStyleDefault;
NSMutableArray* items = [NSMutableArray array];
[items addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow.png"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(locateMe:)]];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Tools"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(toolsButtonTapped:)]];
[toolbar setItems:items];
[self.view addSubview:toolbar];
In my project settings, I have disabled both portrait orientations. I also have this in my root view controller:
// Enforce Landscape Orientation
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
My problem is that the simulator starts in landscape mode, but all of the views are sized for portrait - so the bottom chunk of my views are below the screen and the right side of my screen is a big empty region.
I tried fixing this by switching the width and height of the application frame in the first line, but then that leaves some empty vertical room on the left edge of the screen for the status bar.
So, what's the correct way of doing what I'm trying to do?
Instead of using [[UIScreen mainScreen] applicationFrame]
try using [[[[[self view] window] rootViewController] view] bounds]
The bounds will represent the width and height correctly in Landscape orientation, because the bounds will take into account the transform (rotation) that has been applied, while the frame will not.
To see what I mean, set a breakpoint, and in the debugger print out the description of the top level view lldb> po [[[[self view] window] rootViewController] view]
You'll see that the view has a rotation transform and that its frame does not represent the dimensions of the screen in landscape, but represents the dimensions in portrait!
The long way to calculate the correct applicationFrame would be
BOOL iOS7 = NO;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:#"7.0" options:NSNumericSearch] != NSOrderedAscending)
iOS7 = YES;
CGRect theFrame;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
theFrame.origin = CGPointZero;
theFrame.size.width = screenBounds.size.height;
theFrame.size.height = screenBounds.size.width;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.width; // because we're in landscape orientation
}
}
else {
theFrame = screenBounds;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.height; // because we're in portrait orientation
}
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation))
{
return YES;
}
return NO;
}
Put this code Appdelegate .M.....
Put this in current viewcontroller
// ios 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) {
return YES;
}
return NO;
}
// ios6
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}