Switch Off iPhone Screen - iphone

I am looking for a way to turn off the iPhone screen without the iPhone going to sleep. I don't really mind if turning the screen off is against apple rules. Would setting window alpha to 0 do the trick? Is there maybe some sort of boolean value I can change?
If anyone has an ideas they would be much appreciated.
Many thanks,
Stu

First make the statusbar invisible:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
Then create a subview with the backgroundColor to black:
CGRect rect = [[UIScreen mainScreen] applicationFrame];
UIView *bg = [[UIView alloc] initWithFrame:rect];
bg.backgroundColor = [UIColor blackColor];
[window addSubview:bg];
The reason for making the statusbar hidden first, is so that the [[UIScreen mainScreen] applicationFrame] call will cover the whole screen.

Related

Using launch image as ViewController background image

I created my 3 launch images (Default Default#2x Default-568#2x) at their appropriate resolutions. I'd like to use these images as the background image of my initial View Controller.
However, when I designed my launch images, I did it with the statusbar in mind and left that area blank. When I try to set the background image of the View Controller to the launch image using the following code, my launch image begins at the bottom of my status bar and the blank area is visible. Essentially, my 640x1136 launch image is being squeezed into a 640x1096 space. What is the proper way to do what I'm attempting?
UIImage *backgroundImage = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
backgroundImage = [UIImage imageNamed:#"Default-568h#2x"];
}
else{
backgroundImage = [UIImage imageNamed:#"Default"];
}
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
Update:
I replaced:
[backgroundImageView setFrame:[[self view] bounds]];
With:
[backgroundImageView setFrame:CGRectMake(0 ,-20,self.view.frame.size.width , self.view.frame.size.height+20)];
And it seems to be behaving the way I want now.
Try this:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
Also try not setting backgroundImageView's frame programatically. It should already be the size it should be when you call initWithImage:.
Make use of unedited image which does not contain the blank area. this will work fine.
Check replacing this [backgroundImageView setFrame:[[self view] bounds]]; with the following code.
int statusBarHeight = statusBar.frame.size.height;
backgroundImageView.frame = CGRectMake(0 ,statusBarHeight,self.view.frame.size.width , self.view.frame.size.height-statusBarHeight);

Hiding UIStatusBar / Moving UINavigationBar

I have a navigation-based application where it is possible for the user to hide the status bar. This is remembered in a defaults setting.
It is mostly working, with the one exception that if the app is loaded (from scratch, not returned to after going back to the home screen) when the status bar should be visible, when it is toggled to invisible, the navigation bar does not move up to fill the screen. Toggling the setting after that moves the navigation bar up and down correctly, but still with the extra status bar-sized gap between the navigation bar and the top of the screen, or the status bar. If I return to the home screen and re-enter the application, this corrects itself. I therefore assume there is some method being called on the uinavigationcontroller upon return to the application that I need to call after my toggling of the status bar?
I have tried (I think) all combinations of the wantsfullscreenlayout property, I was setting it in the method below but it made no difference, so I ended up setting it (on the navigationcontroller) to NO in the nib.
Here is the code which toggles the status bar. This is in my application delegate, which has the navigationcontroller and window set up as outlets as per the template application.
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
window.frame = [[UIScreen mainScreen] applicationFrame];
}];
}
else
{
window.frame = [[UIScreen mainScreen] applicationFrame];
}
Thanks for your help.
UPDATE
It seems, via NSLogging, that the problem lies in the frame of the UINavigationBar. So I have added the following code, which works and animates but I am not happy with! I don't feel this can be the "correct" way to do this. In most cases the extra code does nothing since the frame is already at (0,0), but in the one situation where it is incorrect, this gives the right result.
[navigationController.view setNeedsLayout];
CGRect navBarFrame;
UINavigationBar *navBar = nil;
for (UIView *subView in navigationController.view.subviews)
{
if ([subView isMemberOfClass:[UINavigationBar class]])
{
navBar = (UINavigationBar *)subView;
navBarFrame = navBar.frame;
navBarFrame.origin = CGPointMake(0,0);
break;
}
}
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
window.frame = [[UIScreen mainScreen] applicationFrame];
navBar.frame = navBarFrame;
}];
}
else
{
window.frame = [[UIScreen mainScreen] applicationFrame];
}
The window always underlaps the status bar, so you shouldn't try to resize it.
Instead, resize the view of your window's root view controller to [[UIScreen mainScreen] applicationFrame].
the problem lies in the frame of the UINavigationBar. So I have added the following code, which works and animates but I am not happy with! I don't feel this can be the "correct" way to do this. In most cases the extra code does nothing since the frame is already at (0,0), but in the one situation where it is incorrect, this gives the right result.
[navigationController.view setNeedsLayout];
CGRect navBarFrame;
UINavigationBar *navBar = nil;
for (UIView *subView in navigationController.view.subviews)
{
if ([subView isMemberOfClass:[UINavigationBar class]])
{
navBar = (UINavigationBar *)subView;
navBarFrame = navBar.frame;
navBarFrame.origin = CGPointMake(0,0);
break;
}
}
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
window.frame = [[UIScreen mainScreen] applicationFrame];
navBar.frame = navBarFrame;
}];
}
else
{
window.frame = [[UIScreen mainScreen] applicationFrame];
}
As far as I know if you hide the status bar your views automatically occupy the new space and you don't have to change the window frame manually. What happens if you try just this
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
}
This is what solved it for me
Just a View:
[self.view setFrame: [self.view bounds]];
A view with a scroll view inside
[self.view setFrame: [self.view bounds]];
[self.theScroller setFrame: [self.view bounds]];
"theScroller is the name of my scrollview

UIView Fullscreen Problems

I am trying to get my UIView to fill the entire screen. I've tried the code below, which works, but always leaves a white line where the status bar was below. No matter what size i set the frame of the UIView, this line always remains there. What am I missing?
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[view setFrame:[[UIScreen mainScreen] applicationFrame]];
[view setNeedsLayout];
It's highly likely that your view controller's view's frame was set to the applicationFrame and since you're adding the view as it's subview, the frame of your view will be within its parent view's bounds.
You will have to adjust the view controller's view first and then follow it up by adding the view.
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
self.view.frame = [[UIScreen mainScreen] applicationFrame];
view.frame = self.view.bounds;
[view setNeedsLayout];
You will also need to set the autoresizingMask if you think self.view.frame might change.

UIWebView frame to fill screen when loaded landscape and portrait

I am adding a UIWebView to my view, which works fine when I am in portrait. But when I load that same UIWebView in landscape it does not fill the screen. I suspect it comes from how I am setting the frame:
CGRect screen = [[UIScreen mainScreen] bounds];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, screen.size.width, screen.size.height)];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
webView.scalesPageToFit = YES;
Is there a better way to do this so it will load correctly in both landscape and portrait, and fill the screen when rotated?
How sure are you that the parent view is resizing to the full width? UIWebView subview will not go larger than it's parent...
I had same problem.. After i Logged the self.view.frame.size.width i was surprised to find out thats it was 320 instead of 480. The solution to this was :
- (void)viewWillAppear:(BOOL)animated {
[self.view setBounds:CGRectMake(0.f, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
[(UIWebView*)myWebView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];}
Seems that the correct size of self.frame is returned just before showing the view :)

UIViewController not filling screen

I have a UINavigationController that pushes on another UIViewController. In this UIViewController I am going to show a UITableView when in portrait mode and another view in landscape mode.
Therefore in my viewDidLoad I am creating UIView and then adding 2 ViewControllers to this. My problem is that when it loads up I get the following white margin at the top.
I think this is because of (in my Step 3 below ) the
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];
is not returning the full screen, minus the navigation bar. Is this right? If so how can I make it return the full size so there is no white margin?
- (void)viewDidLoad
{
[super viewDidLoad];
// Step 1 - Set up the tableDataView for vertical layout
TableDataViewController *tableController = [[TableDataViewController alloc] init];
self.tableDataViewController = tableController;
[tableController release];
// Step 2 - Set up the graphView for horizontal layout
GraphViewController *graphController = [[GraphViewController alloc]
initWithNibName:#"GraphViewController" bundle:nil];
self.graphViewController = graphController;
[graphController release];
// Step 3 - Get the screen size
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];
// Step 4 - Add the vertical view to the containerView
// and then add the containerView to this controller
containerView = [[[UIView alloc] initWithFrame:appFrame] autorelease];
[[self view] addSubview:containerView];
[containerView addSubview:[tableDataViewController view]];
// Step 5 - Add to the active view so we can test for it later
activeView = [tableDataViewController view];
}
Many thanks
Mike
I think you have an issue with your frame offsets. With the navigation bar enabled the rect you get in appFrame has a y offset of 44.f (the navigation bar's height) - check with NSLog and see if that's true.
Because you are setting the frame of a view that will be placed at the origin it should have x and y origins set to zero. You can do this in a safer manner by checking
CGFloat navHeight = navController.navigationBarHidden ? 0 :
navController.navigationBar.frame.size.height;
In fact I think using the bounds property of [UIScreen mainScreen] may be a better overall solution. It will come with the origin and size set correctly and you shouldn't need to check the presence of the navigation bar.
Check what's going on:
CGRect screenBounds = [[UIScreen mainScreen] bounds]
NSLog(#"%#", NSStringFromCGRect(screenBounds));
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]
NSLog(#"%#", NSStringFromCGRect(screenFrame));
There are a couple things going on. Above all, frame describes a view's location in its superview's coordinate system.
containerView seems to be added as a subview of a custom view, and its coordinate system origins below the navigation bar, not the application frame.
You want to fill the custom view, so the frame of containerView should be something like
CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height);