UIView changing size - iphone

I have a view that I'm creating in the loadview like this:
- (void)loadView {
UIView *mainView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
self.view = mainView;
[mainView release];
}
So, if I print the view I got the result:
>
OK, that's what I wanted, 767x1024
The problem is, if I call this method:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
and present a modalViewController and then dismiss it, on my previous view controller, printing the method viewDidAppear and viewWillAppear I got this:
viewWillAppear:
[<UIView: 0x4e946a0; frame = (0 0; 768 1024); layer = <CALayer: 0x4e946d0>>]
viewDidAppear:
[<UIView: 0x4e946a0; frame = (0 0; 768 1004); layer = <CALayer: 0x4e946d0>>]
Why the view size is changing by 20? I know that has something to do with the status bar, but can't figure it out.
Thanks.

You are right. Status bar default size is 20 pixel in height. UIViewController automatically adjust child view size as status bar appears. According to apple documentation "When a view controller is displayed on screen, its root view is typically resized to fit the available space, which can vary depending on the window’s current orientation and the presence of other interface elements such as the status bar." — http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Related

Status Bar Height During viewDidLoad

I have been using the method below to get the status bar height. However I need to get the height when the view loads so I can adjust the height of my tableview accordingly. Using this method in viewDidLoad returns 0.
I have tried using it in viewDidAppear which works, however when I return to the view from a child view (as its part of a nav controller) it takes the height and adjusts it again, every time I return to the view.
So how can I get the height when the view loads to apply this adjustment without running into these issues?
-(CGRect)statusBarFrameViewRect
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect statusBarWindowRect = [self.view.window convertRect:statusBarFrame fromWindow: nil];
CGRect statusBarViewRect = [self.view convertRect:statusBarWindowRect fromView: nil];
return statusBarViewRect;
}
Thanks.
I was able to get the height of both the Status Bar and the Navigation Bar in -viewDidLoad. If you are also transitioning in/out the status bar while loading this new view, you may want to register for UIApplicationWillChangeStatusBarFrameNotification. I can even get the status bar frame in -[appDelegate application:didFinishLaunchingWithOptions:]
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
// (CGRect) $1 = origin=(x=0, y=0) size=(width=320, height=20)
CGRect navBarFrame = self.navigationController.navigationBar.frame;
// (CGRect) $2 = origin=(x=0, y=20) size=(width=320, height=44)
}
As an aside, I'd expect individual views to not have to care about the status bar, specifically, so this leads me to think you might be using the status bar as an overlay. If that's the case (Photos.app does this), consider adjusting the contentInset instead of height.

View isn't rotating properly

I have this code inside a UIViewController subclass:
- (id)init {
self = [super init];
if (self) {
self.view.frame = [PDToolbox screenFrame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor redColor];
}
return self;
}
The only thing I have in terms of any rotation methods is this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
Yet after the screen is rotated to landscape, doing an NSLog on the view shows this:
<UIView: 0x10061640; frame = (0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x10061670>>
I don't understand why it's doing this transform thing, and not just rotating like normal? It means any views i place on top of it after the rotation and set to be the size of the view end up at a 320x480 position.
EDIT:
People aren't understanding. So I put a view on top of it, the same size as the UIView, using:
UIView *anotherView = [UIView alloc] initWithFrame:controller.view.bounds];
[controller.view addSubview:anotherView];
If I add anotherView in in portrait, anotherView appears in portrait, with the frame 320x480.
If i add anotherView in in landscape, anotherView appears in landscape, but still with the frame 320x480, becaus that's what the controller.view's frame is still, for some unknown reason.
What does your view/controller hierarchy look like? It looks to me like something is setting a 90° rotation transformation on your view, rather than changing the view's frame. If you're not doing that yourself, it's likely a parent view or view controller.
Frame and bounds are very different things. You should read Apple's guide to View Geometry - it contains a lot of information that's been invaluable to me.
Your bounds is always going to be 320x480 because someone is setting your transform (which affects the frame, but not the bounds).
Make sure your view controller's view's superview has autoresizesSubviews set to YES and that any subviews added have appropriate autoresizingMasks.

touchesBegan after didRotateFromInterfaceOrientation

I noticed that after an orientation change from portrait to landscape, I'm not getting touchesBegan events for some parts of my view any longer. I suppose that this is because I'm not informing my UIView about the dimension change of my window's frame after the device rotation.
I'm setting up everything programmatically (UIWindow, UIViewController, UIView) like this:
myViewController = [[myUIViewController alloc] init];
myWindow = [[UIWindow alloc] initWithFrame: rect];
myView = [[myUIView alloc] initWithFrame: [myWindow bounds]];
[myViewController setView:myView];
[myWindow addSubview:myView];
[myWindow setFrame:rect];
[myWindow makeKeyAndVisible];
When I get the didRotateFromInterfaceOrientation notification, I'm updating the window frame like this:
[[[self view] window] setFrame:rect];
But after that, my UIView does no longer get touchesXXX events for all areas. It seems that only the areas of the previous frame are still reporting events. So my question: Is there anything else I need to do in didRotateFromInterfaceOrientation to inform my UIView about the dimension change?
Thanks for help!
EDIT: Do I have to reposition the UIView on didRotateFromInterfaceOrientation() or is this done automatically? I noticed that the "transform" property of my UIView is set to a transformation matrix when the orientation changes. However, this makes it very hard to reposition my view. The docs say that the "frame" property can't be used when a transformation is active, so I tried to modify the "center" property to reposition my view, but this also doesn't work correctly. I want to move the view to the top-left corner, so I set "center" to (screenwidth/2,screenheight/2) but it doesn't position the view correctly :( Any idea or info what must be done to get the events right in orientation mode?
I have had this same problem, and I believe it is a frame issue.
I had to manually set the rects depending on orientation, as well as set the userInteractionEnabled on the main view, like:
if (appOrientation == 0)
appOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (appOrientation == UIInterfaceOrientationLandscapeLeft || appOrientation == UIInterfaceOrientationLandscapeRight) {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
myView.frame = CGRectMake(0, 0, 1024, 768);
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
myView.frame = CGRectMake(0, 0, 768, 1024);
}
myView.userInteractionEnabled = YES;
OK I know this is an old question but this is how I fixed this issue.
In my situation I had a storyboard with a view that would be displayed either in portrait or forced to landscape mode depending on a user setting.
My app displays the statusBar at all times except for when I'm showing this view.
To make this all work, the transformations had to be applied in the viewWillAppear method for one. I had the following code in the viewDidAppear method at first and that messed with the bounds for the touchesBegan event I guess.
Here's the viewWillAppear code:
- (void)viewWillAppear:(BOOL)animated
{
// Hide the status bar
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// This is to offset the frame so that the view will take the fullscreen once the status bar is hidden
self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
// If view is to be displayed in landscape mode
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"orientation"])
{
// Change status bar orientation
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft];
// Turn the view 90 degrees
[self.navigationController.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];
// Set the frame and the bounds for the view
// Please Note: Frame size has to be reversed for some reason.
[self.navigationController.view setFrame: CGRectMake(0,0,320,480)];
[self.navigationController.view setBounds: CGRectMake(0,0,480,320)];
// Make sure user can interact with view
[self.navigationController.view setUserInteractionEnabled:YES];
}
}
Any other thing that had to happen layout wise, have to happen in the viewDidAppear method. For instance I had an image that covered the whole view and the frame had to be set depending on the orientation. Setting the frame in the viewWillAppear gave weird results but the same code worked perfectly in viewDidAppear.
Hope this helps someone as I banged my head for 6 hours on this thing.

How In-Call status bar impacts UIViewController's view size?

I'm trying to understand how the view associated to a UITabBarController, UINavigationController or UIViewController reacts when the in-call status bar is toggled.
My trouble is that they seem to behave differently and this causes me side effects.
I've made a project that changes the root view controller of the window for the 3 types above and I dump the description of the view to get the frame coordinates.
UIViewController
inCall status OFF:
UIView: 0x4e2a1f0; frame = (0 20; 320 460); autoresize = W+H; ....
ON
UIView: 0x4e2a1f0; frame = (0 40; 320 440); autoresize = W+H; ...
This one I understand : when the in-call status bar appears, the height of the view of the UIViewController shrinks and looses 20, and its y coord moves from 20 to 40.
That's perfect ! I would expect the same when replacing a classic UIViewController with a UITabBarController or a UINavigationController but that's not the case !
UINavigationController
InCall status bar OFF
UILayoutContainerView: 0x4b35ab0; frame = (0 0; 320 480); autoresize = W+H; ..
ON
UILayoutContainerView: 0x4e1b060; frame = (0 0; 320 480); autoresize = W+H; ..
In that case, the view handled by the UINavigationController does not have its frame properties changed when the in-call status bar is toggled?! (why ? :( )
UITabBarController
OFF
UIView: 0x4b2f6a0; frame = (0 20; 320 460); autoresize = W+H; ...
ON
UIView: 0x4b2f6a0; frame = (0 20; 320 460); autoresize = W+H; ...
Same as in the UINavigationController: the view of the UITabBarController does not seem to be impacted when the incall status bar is toggled.
Can someone explain me how this resize works when displaying the incall status bar appears ?
My end goal is to display a UIView that is shown ABOVE the whole UITabBarController and that resizes properly when the in call status is displayed. However, I really don't know where to put such a view in the views hierarchy : if I add it as a child of the UITabBarController's view, as this one does not react to the incall status display, mine does not react as well :(
The height of the view when the In-call status bar is toggled depends on the way it's anchored.
Play around with the autoResizingMask of the UIView to control whether the view should move down or resize when the in-call status bar shows up.
These two properties,
UIViewAutoresizingFlexibleTopMargin
UIViewAutoresizingFlexibleHeight
will help you. The first one pushes the view down, the second one changes the size.
Your regular UIViewController example has [wantsFullScreenLayout] set to NO, which causes the view to be automatically sized so it doesn't go under the status bar.
UINavigationController and UITabBarController, on the other hand, default wantsFullScreenLayout to YES. So their views take up the whole window, and they size and position their subviews themselves to appropriately handle the status bar. If you explicitly set the property on these controllers to NO, you should get the behavior you desire (but will then lose the ability to properly handle child controllers that set wantsFullScreenLayout to YES, if you care about that).
In your UITabBarController example, BTW, it seems that you are not printing the information for the view of the tab bar controller; here that is a UILayoutContainerView, not a plain UIView.
Ideally you should forget about how much amount the view gets resized and play around with Autoresizing mask.

iOS -- how do you control the size of a modal view controller?

I am presenting a modal view controller. If it matters, it is scrolling up from the bottom. How can I control what portion of the screen it occupies?
EDIT: I have the following in the modal view controller. It's not helping.
- (void)viewDidLoad {
TestResultView *trv = [[TestResultView alloc]initWithTest: [Model m].currentTest];
self.view = trv;
trv.frame = CGRectMake(0, 320, 320, 160);
[trv release];
[super viewDidLoad];
}
You can modify the frame of the view controller, but if you're using UIViewController's -presentModalViewController:animated: method, the view behind will be unloaded once your modal view is finished animating onto the screen (This assumes you're on an iPhone) and you'll see a white screen where your background view should be. iOS assumes that your modal view controller will be a full-screen view controller, and dumps the other view to save memory.
If you really want to show a view over part of the screen, you should instead add the UIView (no UIViewController) to your current UIViewController's view as a subview, and then animate it onscreen yourself. I think something like this would work in your UIViewController class that will present the view:
// Add the view as a subview and position it offscreen just below the current view
UIView *myHalfView = [[UIView alloc] initWithFrame:someAppropriateFrame];
[self.view addSubview:myHalfView];
CGRect offScreenFrame = myHalfView.bounds;
offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));
// Now animate the view upwards
[UIView beginAnimations:nil context:nil];
// Move the view upwards the height of your sliding view so it's entirely onscreen
myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height);
[UIView commitAnimations];
[myHalfView release];
For bonus points, you could fade the view in by setting
myHalfView.alpha = 0.0;
before the UIView animation block, and setting
myHalfView.alpha = 1.0;
inside the block after animating the center property.
When you're done, you can do something similar but in reverse to slide the view offscreen. You can add an animationDidStop selector to the UIView animation block to be notified when the view has slid off screen so that you can remove it from the view hierarchy.
From an aesthetic point of view, you should also be careful how you do this since having a view slide up is a standard behavior, and if your view looks like a normal view but stops halfway, users may feel (even briefly) that the app has frozen. They'll figure it out, but it will leave a bad feeling about your app if not handled carefully. Mainly, I would avoid using standard full-screen cues like including a UINavigationController at the top of your view to help users understand what's going on. Half-sheets tend to be UIActionSheets on the iPhone, so think in that direction.
That is nice, the above accepted answer explains a nice hack to present subViews which feel like ModalViews, but what if it is an iPad, and i can indeed give it a modalViewController which doesnt cover the entire screen.
In case of iPads, I dont think the underneath view will be unloaded. ( because there are options where we can present the modalView on iPads, which dont cover the entire screen )
ModalViewController in the end is a controller itself, and like any other controller has a root view, whose properties can be editted, if we can get hold of it.
Here is what will give you a custom frame of the ModalView :
MyViewController *viewController = [[MyViewController alloc] init];
viewConroller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
//superView of viewController's view is modalViewController's view, which we were after
viewController.view.superview.frame = CGRectMake(x,y,w,h);
//x y w h - can have desired values.
I would add to #dsaw's answer that the superview of the modal view does not seem to rotate its coordinate system in landscape mode. Here is the code that I used in my own app:
MyViewController* modalVC = [[MyViewController alloc] init];
modalVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalVC animated:NO];
CGRect r = CGRectMake(self.view.bounds.size.width/2 - 236,
self.view.bounds.size.height/2 - 130,
472, 260);
r = [self.view convertRect:r toView:modalVC.view.superview.superview];
modalVC.view.superview.frame = r;
While the superview may not rotate itself with the iPad, it does seem to do the right thing and keep the modal view centered if I rotate the iPad after showing the modal view.