Hide Status bar with if statement? - iphone

I have a QR code reader in my app. Once the reader scans a code, it takes the app to a survey page. I'm trying to get the survey page to hide the statusbar. Here is my code:
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
// self.resultsToDisplay = result;
if (self.isViewLoaded) {
[[NSBundle mainBundle] loadNibNamed:#"yellaViewController" owner:self options:nil];
initWithNibName:#"yellaViewController" bundle:[NSBundle mainBundle]];
[topImage setImage:[UIImage imageNamed:#"yellalogoREAL.png"]];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
This isn't working for me, and the statusbar stays visible. What am I doing wrong?
ALSO: Is there a way I can hide the tabbarcontroller on the surveypage using the same if statement?

In ZxingController's viewDidAppear: (ZxingWidgetController.m)
self.isStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
if (!isStatusBarHidden)
[[UIApplication sharedApplication] setStatusBarHidden:YES];
it cached the previous statusbar state, and when you exit the ZxingController, in viewDidDisappear:
if (!isStatusBarHidden)
[[UIApplication sharedApplication] setStatusBarHidden:NO];
Since the viewDidDisappear in ZxingController will enter after zxingController:didScanResult:
So your setStatusBarHidden in zxingController:didScanResult: is no use.

Related

ios 7 document interaction controller hide status bar

In my iOS app i've hidden the status bar with this code in each ViewController:
- (BOOL)prefersStatusBarHidden
{
return YES;
}
In a view I need to use a UIDocumentInteractionController, but when it comes up, the status bar appears, is there a way to keep it hidden?
Thanks in advance
Use a combination of the following code and the code from iOS:
- (UIViewController *) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *) controller {
// hack to keep status bar visible
[[NSOperationQueue mainQueue] addOperationWithBlock:
^{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}];
return self;
}
combined with
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Try this works for me :
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Set documentController.delegate to self and use
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}

Why MBProgressHUD not show, when an alert show before HUD show?

I use this code to init MBProgressHUD
UIWindow *window = [[UIApplication sharedApplication] keyWindow]
_hud = [[MBProgressHUD alloc]initWithWindow:window];
_hud.dimBackground = bDim;
_hud.labelText = message;
[window addSubview:_hud];
[_hud show:YES];
but sometimes _hud not show in window ?
Anyone let me know where im lack here??
Thanks!
Click here!
This works for me .Just use this window.
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
Or
you can use
self.navigationController.view
to add you HUD.

Hide the status bar on iPhone on a single view?

I want to show the status bar in my app in all views but one. I have tried modifying the 'status bar is initially hidden' in the plist, i have tried:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
That hides the bar but leaves an ugly blue box where the status bar was (which isn't part of my view, there's nothing blue on there).
I have also tried altering the layout wants full screen and status bar settings in the 'interface builder' bit of Xcode 4.2.
Any suggestions?
EDIT - SORT OF SOLUTION:
I have done it by including:
-(void)viewWillDisappear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
-(void)viewDidAppear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
on every single page that I want the status bar to be on.
It still looks choppy and rubbish because the tab bar appears and reappears each time you switch a view. But i've had enough, worked on this stupid problem for about 5 hours now so this will have to do.
SECOND EDIT -
Fixed the choppyness by including setStatusBarHidden=NO in viewWillAppears. God knows how everything works but it does.
Try This one It will Run perfectly..
[[UIApplication sharedApplication] setStatusBarHidden:YES];
And in XIB set none option for the status bar.
for iOS 7.
Go to info.plist and add two attributes if not present. set "Status bar is initially hidden" to "YES" and set "UIViewControllerBasedStatusBarAppearance" to "NO". This will hide status bar for your app.
#pragma mark - Hide statusbar
-(void)hideStatusBar {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
// iOS 7.0 or later
[self setNeedsStatusBarAppearanceUpdate];
#else
// less than 7
[[UIApplication sharedApplication] setStatusBarHidden:YES];
#endif
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
If there is anyone looking for a solution where the above solution does not work (and there is still an annoying blue 20px gap at the top), try putting this in the viewWillAppear on the implementation file of the view controller that you would like the status bar to be hidden.
self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
That literally took me 12 hours or so to fix, and that was the solution, so now i'm spreading the word in case anyone else has this annoying issue.
Kartik's solution worked for me.
[[UIApplication sharedApplication] setStatusBarHidden:YES];
I added this to viewWillAppear: instance method.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.co.uk"]]];
}
And I spent ages on this too. Using Xcode 4.2, iOS5 sim.
But when I first implemented it, the annoying "space" at the top was there. I selected the View Controller in the storyboard and set the properties as follows:
Size: Full Screen
StatusBar: None
everything else inferred.
I checked wants full screen.
Voila, it all worked fine.
I know this is an old question but none of this answers works for me, so this is how it works for me to hide the status bar in a single viewController
First in your parentViewController you have to set:
- (UIViewController *)childViewControllerForStatusBarHidden {
if ( hideStatusBarViewController ) {
return hideStatusBarViewController;
}
return nil
}
It only returns the child view controller when its created otherwise nil is the default. When you add your hideStatusBarViewController you must call
[self setNeedsStatusBarAppearanceUpdate];
on the parentViewController, this function forces to read childViewControllerForStatusBarHidden. Finally in hideStatusBarViewController you must set
- (BOOL)prefersStatusBarHidden {
return YES;
}
Its the only solution that works for me. I hope it help somebody.
I would suggest you a different approach: insert that view onto the application's window:
YourUIAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window insertSubview:self.yourView atIndex:([[appDelegate.window subviews]count])];
That way it will show over the status bar
I hope it helps you
This is solution if you want to hide status bar on a single view
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.view sizeToFit];
}
Here is a snippet of code that might help. When the view loads show the status bar, when you leave the view hide it again.
-(void)viewWillAppear:(BOOL)animated {
if([[UIApplication sharedApplication] respondsToSelector:#selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated {
if([[UIApplication sharedApplication] respondsToSelector:#selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
else
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
The [[UIApplication sharedApplication] setStatusBarHidden:BOOL]; is enough, but:
Remember to set it back to NO before leaving the view or the page you return to will likely have the Nav Bar under the status bar.
Make sure that you set both status bar hide and show in the view in which you want the status bar hidden. I did the switch off in the viewDidLoad method, and - crucially - the switch back on in the viewWillDisappear:animated method. Any later and you're in for trouble.

How can i give the external link to UIButton?Is it possible?

I am having the UIButton name as "Buy now".If any one touch the button,the external link should open in the safari browser.How can i achieve this?
It's easy. You set the target and selector for the button, then inside the selector, you call safari to open your link.
Code to call Safari:
Objective-C
- (void)buttonPressed {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString: #"https://www.google.co.uk"]];
}
Swift 2.x
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.co.uk")!)
Swift 3.x
UIApplication.shared.openURL(URL(string: "https://www.google.co.uk")!)
Create a button, and give it a target to a selector that opens Safari with the link.
Basic example:
Make a UIButton
UIButton *button = [[UIButton alloc] initWithFrame:...];
[button addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
Then the method to open the URL
-(void)someMethod {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.ca"]];
}
Don't forget to give your button a proper frame and title, and to add it to your view.
- (IBAction)buyNowButtonPressed {
NSString *s = [ NSString stringWithFormat:#"http://www.sample.com/buynowpage"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:s]];
}
openURL is deprecated from iOS 10 and use following instead.
UIApplication *application = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:#"http://www.yourDomain.com"];
[application openURL:url options:#{} completionHandler:nil];

UIApplication sharedApplication - keyWindow is nil?

I want to convert a CGPoint from my UIView to UIWindow coordinates and have realized that UIApplication keyWindow is always nil; why is this?
I have tried the convertPoint:toView: method from UIView.
Please see this sample code I tried in the view controller in a template of Xcode (View application):
- (void)viewDidLoad {
[super viewDidLoad];
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(40,40,250,250)];
[test setBackgroundColor:[UIColor redColor]];
[self.view addSubview:test];
CGPoint p = CGPointMake(100, 100);
CGPoint np;
np = [test convertPoint:p toView:[[UIApplication sharedApplication] keyWindow]];
NSLog(#"p:%# np:%#", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
np = [test convertPoint:p toView:[appDel window]];
NSLog(#"p:%# np:%#", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
np = [test convertPoint:p toView:nil];
NSLog(#"p:%# np:%#", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
[test release];
if(![[UIApplication sharedApplication] keyWindow])
NSLog(#"window was nil");
}
and I get:
p:{100, 100} np:{100, 100}
p:{100, 100} np:{140, 160}
p:{100, 100} np:{100, 100}
window was nil
I can convert it but only when I access the window through the app delegate. And not UIApplication. According to the documentation, keyWindow should work here, but is nil.
Why is this?
This code was executed before [window makeKeyAndVisible]; which is inside the app delegate.
So, no wonder why keyWindow was nil yet.
Easiest way is to get the window from the app delegate instead:
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now
I noticed that after having started the Guided Access, the keyWindow property on [UIApplication sharedApplication] appears to be nil.
It happened to me only on iOS7 when starting the Guided Access Mode for the first time after having enabled it in Settings > General > Guided Access, so the starting GAM view is actually displayed and not by-passed.
Since this Apple API seems buggy, I solved using the following code to retrieve the window I'm looking for.
NSArray *windows = [[UIApplication sharedApplication] windows];
if ([windows count]) {
return windows[0];
}
return nil;
Instead of
[[UIApplication sharedApplication] keyWindow];
maybe you could also try using
[[[UIApplication sharedApplication] delegate] window];
as iWasRobbed pointed out but it wasn't working for me as the rootViewController property isn't reachable this way.
Try this, first get the UINavigationController handle, and then the topViewController
let navController = window?.rootViewController as! UINavigationController
let yourMainViewController = navController.topViewController as! ItemsViewController
or
let yourMainViewController = navController.viewControllers.first as! ItemsViewController