I want the view of my current UIViewController to take the whole screen (without the status bar) when I hide the UINavigationBar. It doesn't have to be animated. I can't figure out how to change the frames. I should mention also that I am using a nib, so in the Interface Builder I have selected Simulated Metrics : Navigation Bar, I don't know if it makes any difference.
Try this:
// On YourViewController.m
// Call this method when the nav bar gets hidden
- (void)navigationBarDidBecomeHidden {
self.view.frame = CGRectMake(0, 0, 320, 480);
}
Related
I am trying to emulate the way TweetBot/NetBot animates the tabBar in after a push from the tableView of Accounts action. When the view is fully pushed, only then does the taBar animate in from the bottom. I have tried all sorts of hide/show methods and all seem to fail when it comes to the "show" part.
Does anyone have a suggestion as to how this can be done?
First of all, I presume you are not using a UITabViewController since it cannot be pushed into a UINavigationController stack, so I think you are using a standalone UITabBar embedded in a UIViewController. Is this assumption right?
Try with this code (I didn't try it).
- (void)viewDidAppear {
[super viewDidAppear];
// Calls showTabBar method after SOME_DELAY. You can also call directly [self showTabBar] if you want zero delay.
[self performSelector:#selector(showTabBar) afterDelay:SOME_DELAY];
}
- (void)showTabBar {
// Before the animation begins, your UITabBar must be outside the view controller's view frame.
CGRect tabBarFrame = CGRectMake(0,
CGRectGetHeight(self.view.bounds),
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.tabBar.frame);
self.tabBar.frame = tabBarFrame;
// Let's start with the animation, setting a new frame for tab bar inside an animation block
[UIView animateWithDuration:ANIMATION_DURATION animations:^{
// Change origin Y. It assumes that the height of self.tabBar is right, otherwise put the height you want instead of CGRectGetHeight(self.tabBar.frame).
tabBarFrame.origin.y = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.tabBar.frame);
self.tabBar.frame = tabBarFrame;
}];
}
I am trying to add a the UINavigationController UIToolbar to the top of the view, (under the navigation controller.
My view is a UITableViewController... so there is that to deal with. Currently I am just positioning the UIToolbar that appears at the bottom of the view where I want it to display using
[self.navigationController.toolbar setFrame:CGRectMake(0, 60, 320, 30)];
this positions the toolbar in the correct place I would like it to appear, However there is a problem with where its positioned, which I will explain.
When you set a UINavigationController toolbar to be displayed it puts itself at the bottom of the view and pushes the UITableView up so the toolbar does not cover the tableview. However when I change the position of the toolbar the tableview still thinks the toolbar is at the bottom of the screen meaning the toolbar does not meet flush at the bottom of the screen how I would like it too.
So my question is how can I get the toolbar to display directly below the navigation controller bar and push the tableview down abit to accommodate for the toolbar in its new position.
I hope this all makes sense, Any help I would like to than in advance and below is the current code I am using (all be it basic I am still abit perplexed about whats going on behind the scenes for this to happen).
- (void) viewDidLoad
{
//..
[self.navigationController setToolbarHidden:NO animated:YES];
[self.navigationController.toolbar setFrame:CGRectMake(0, 60, 320, 30)];
self.navigationController.toolbar.tintColor = [UIColor lightGrayColor];
//..
}
update
this is currently what the toolbar is doing to my tableview
Call setFrame on your UITableView to move it into positon.
float y = self.navigationController.toolbar.frame.origin.y + self.navigationController.toolbar.frame.size.height;
[myTable setFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height-y)];
I have an iPhone application that's using Navigation Controller to display the top bar (with title and back button, and such...).
I added a UITabBar to the application window, that enables to switch between the parts of it. Instead of adding the tab bar to each of ViewController's view I added the bar to app window.
(When I had it in the ViewController, switching between controllers made the tab bar to swipe left/right, when animated pop/push occured, together with whole view).
So, I added the UITabBar to the MainWindow.xib, and tied it to the app delegate's variable. In my didFinishLaunchingWithOptions method, I added the following code:
[self.window addSubview:navigationController.view];
CGRect frame = navigationController.view.frame;
frame.size.height -= tabbar.frame.size.height;
navigationController.view.frame = frame;
tabbar.selectedItem = [tabbar.items objectAtIndex:0];
to resize the main (navigationController's) view, in order to make the TabBar visible.
The problem shows up when I rotate the device -- my view gets stretched to full window and I loose the ability to show the TabBar.
I added a - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation method to my ViewController, with the following code:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
CGRect frame = self.view.frame;
frame.size.height -= [AppState shared].tabBar.frame.size.height;
//frame.origin.y = [AppState shared].tabBar.frame.size.height;
//frame.origin.x = 100;
self.view.frame = frame;
frame = [AppState shared].tabBar.frame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.origin.y - frame.size.height;
[AppState shared].tabBar.frame = frame;
}
It resizes the view, and moves the tab bar to up/down part of the view (I allow only Portrait/Portrait upside down orientations here). The problem is, my TabBar is turned upside down as well, and also, it's no longer clickable.
It looks like the image below:
Anyone knows how to handle this kind of situation? Or, how to make the tab bar not tied to view controller, but also able to handle interface's rotation smoothly?
You are using the tabbar in an unintended way. You seem to be using the UITabBarView as an uncontrolled element of other views. That is not it's function.
The UITabBarView should be controlled directly by a UITabBarController which in turn should be controlling all the view controllers for the views displayed in the tabbar i.e. the tabbar controller is a type of navigation controller that controls subcontrollers.
Suppose you have three tabs and the third one is a navigation controller. Your controller hierarchy would look like this:
TabbarController:
-->tab1ViewController
-->tab2ViewController
-->tab3ViewController(UINavigationController):
-->rootViewController-->secondViewController
You are trying to move and manage the tabbar view without its controller and the proper controller hierarchy. That isn't going to work.
If I use a UINavigationController to display my UINavigationBar, this bar is much slimmer in landscape mode than in portrait mode.
Everything fine so far, as I want this behavior, but I have one view which isn't handled by an UINavigationController and so I just dragged and dropped a UINavigationBar from Interface Builder into the view but this one has always the same size and I can't see a way to tell the UINavigationBar that it should resize.
Anyone knows how to achieve this?
You can resize the the navigation bar in the layoutSubviews method of your UIView, like so:
- (void)layoutSubviews {
[super layoutSubviews];
// Let navBar tell us what height it would prefer at the current orientation
CGFloat navBarHeight = [navBar sizeThatFits:self.bounds.size].height;
// Resize navBar
navBar.frame = CGRectMake(0, 0, self.bounds.size.width, navBarHeight);
}
Here is my setup: one navigation controller, two views
The first view, the rootview, displays the statusbar and navigation bar (portrait view).
When the rootviewController senses rotation, it hides the nav bar and status bar and then it pushes view 2 onto the navigation controller (in landscape now).
This part works as expected.
When View2 viewcontroller senses rotation (back to portrait), it pops itself of the navigation controller, revealing view 1. View 1 then unhides the status bar and nav bar.
From here, it gets weird. Depending on when I unhide the navigation bar, I get strange results. As you can see below (The beaker photo should be just below the navbar at the top of the screen).
I have tried unhiding the navigation bar in:
viewWillRotate/viewDidRotate of view 2
viewWillAppear/viewDidAppear of view 1
poptoRootView in the navigation controller (I subclassed just to try)
Nothing works. Any Idea what is going on? This should be simple, but maybe I am doing things in the wrong places.
Check your autoresizing mask on all views in your nib and make sure its all set properly. If you have anything the way its not supposed to be it will freak out on rotation.
Not sure why it's necessary, but when you're swaping views, you must apply a transformation to your view (with only 1 view the iPhone does this for you), and you must set the bounds of it.
Here is the code that should work for you (on the willAnimateFirstHalfOfRotationToInterfaceOrientation):
#define degreesToRadians(x) (M_PI * (x) / 180.0)
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
self.view = landscapeView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 320);
} else {
self.view = portraitView;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
self.view.bounds = CGRectMake(0.0, 0.0, 300, 480);
}
In the end, I reworked my app. (to use a modal view controller)
But I came into similar issues, instead white space where the status bar was located.
I think both problems can be attributed to not talking to the Navigation Controller when rotating/resizing views (instead I was talking to the ViewController.
For details of how I solved the problem:
iPhone + CGAffineTransFormRotate(pi/2) + statusBarHidden:YES + presentModalViewController = 20 pixels of white space