UIView Fullscreen Problems - iphone

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.

Related

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

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);

How to make UIScrollView respect the layout of the containing UIView?

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.
Here's the relevant code from the UIViewController:
- (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame];
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame];
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];
CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];
//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];
//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];
//turn on paging
[sv setPagingEnabled:YES];
UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];
}
Thank you in advance for any advice or help.
I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:
CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);
contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.
You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.
Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.
Instead, use any sane value for full frame and enable autoresizing:
CGRect fullFrame = {{0,0}, {320,480}};
...
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.

Switch Off iPhone Screen

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.

iPhone - flipping views shows a white background

I am using the flip animation to animate between two views in my viewcontroller. The problem is that the background shows a white blank background while the animation is taking place. I would like to show a black background.
I tried setting the background color of the main view to black both in IB and code. But the background is still white.
Can someone please help me.
Thanks.
Adding the code
[self setContentView:[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]];
contentView.backgroundColor = [UIColor blackColor];
[contentView addSubview:toolbar];
[self setView:contentView];
[contentView release];
frontView = [[FrontView alloc] initWithFrame:viewFrame];
[frontView setViewController:self];
[self.view insertSubview:frontView belowSubview:toolbar];
//Initializing the back view here too
//on button click, executing normal flip code
Even after this I get a white background
I think your issue might be that the UIWindow is shown during the animation. To fix this issue, set the background color of your main window. You can do this in code or in IB.
You begin by creating a fake main view, and set its background to black:
// Create the main view
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
Then, you create your front and back views, and add them to your main view:
// create front and back views
UIView *frontView = ...
UIView *backView = ...
If you are using IB, skip the previous step and add directly your views
// add the views
[self.view addSubview:backView];
[self.view addSubview:frontView];
Now do the flip animation as usual.
EDIT: Probably it does not work because in your code you are adding the frontView below the toolbar. Add first the backView, then the frontView and finally the toolbar using the addSubview: method. Then, use the following code to animate the flip:
- (IBAction) flipView{
// Start Animation Block
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
// Animations
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// Commit Animation Block
[UIView commitAnimations];
}
Since the code performs [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; the order in which you add the subviews is relevant.