Frame of main View of UIViewController inside a UINavigationViewController - ios

I am confused by the frame of self.view within a ViewController I have made the root ViewController of a UINavigationViewController. Lets say I add a red square UIView to self.view (where self.view is the embedded ViewController's view property). This red view is mostly occluded by the navigation bar. In other words, why doesn't self.view's frame's origin.Y = HeightOfNav bar? Instead it is 0.
I have read documentation that states a view added to the navigation controller resizes to take the nav bar's height into account, however, all my testing has shown this isn't the case. Not at any point in the view lifecycle does my view correct its origin to be visible. Perhaps I am setting up this viewcontroller/navigationController incorrectly such that the appropriate view-resizing does not take place but you can read the subsequent code to see if that is the case.
Here is the simple code where I generate these view controllers in appdelegate:
UIViewController * a = [[ViewController alloc] init];
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:a];
self.window.rootViewController = nav;
Logging out self.view's frame in a's viewDidLoad, viewWillAppear, viewDidAppear, etc.. shows { 0, 0, screenWidth, screenHeight } (where screen* is the current simulator). So, 0,0 is behind a navBar and this is not the behavior I would expect.
Again, my expectation was that self.view would now have a frame whose origin starts after the NavigationBar at the top of any view inside a UINavigationController.

Related

Is it possible to change frame of view controller in UITabBarController so that UITabBarController view will be visible?

I have my own subclass of UITabBarViewController.
Is it possible to change frame for all embedded viewcontrollers' views so that own UITabBarViewController view will be visible partially?
On the attached image I set purple color for own tabBarController view.
I want to change frame of each selected view controller so that this purple view (UITabBarController view) will be visible.
I stumbled upon this answer looking for a solution myself, and found an okayish way to handle this: wrap your viewController inside another viewController as a childViewController.
Essentially, you would present a viewController with clear background, which has your content controller as childController with a frame you want it to have:
UIViewController *wrapperController = [UIViewController new];
wrapperController.backgroundColor = [UIColor clearColor];
[wrapperController addChildViewController:vc];
[wrapperController.view addSubview:vc.view];
vc.view.frame = CGRectMake(...);
Just make sure to pass the tabbarItem to the wrapper, and use that one instead of the child.

UINavigationController push viewcontroller with hidden navigationbar

I have a UINavigationController and its navigationBar hides if the user scrolls (done with UIScrollViewDelegate, not with pan gesture).
But when I push a a new viewController onto the stack, the new controller's view's origin y is at 64px and not where the old topViewController's y was (which is 21px).
How can I make the push, so that the new controller appears at the same position as the old one was?
The only place that updating the view's frame was successful was in viewDidAppear, but that's to late, because the user can see a black space on the top for a second.
Updated:
It seams like it has to be done between viewWillAppear and viewDidAppear, but I can not find reasonable method to do this.
Tried to alter the UINavigationController's view (currently I'm just moving it's navigationBar), but that messes up the layout.
Update again:
Some more info, because none of the answers and comments helped. The navigationcontroller is instantiated from storyboard. It's a custom UINavigationController, which hides the navigationBar on it's topViewController's scrollView scrolling (nothing else is overwritten).
The first topViewController (from which the push is started) is a UIViewController with 2 container view (one if logged in, other if not). The second topViewController (the one which is pushed onto the stack) is a UIViewController with a full screen UIWebView.
Everything is coming from the storyboard. All the Extended Edges checkboxes are off for every viewController and all the navigationBars are opaque. AutoLayout is used everywhere and (I think setup right).
--Edited--
change your frame in viewWillAppearmethod
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.view setFrame:CGRectMake(0, 21, self.view.frame.size.width, self.view.frame.size.height)];
}
or setFrame of your newController before to be pushed.
NewController *newC = [NewController alloc] initWithNibName:#"NewController" bundle:nil];
[newC.view setFrame:CGRectMake(0, 21, self.view.frame.size.width, self.view.frame.size.height)];
[self.navigationController pushViewController:newC animated: YES];

Frame sizing of tableview within nested child controllers/subviews

I'm a bit confused by the proper frame sizing of a table view to fit within my screen.
Here's my setup of view controllers within view controllers:
UITabBarController
UINavigationController as one of the tab bar viewcontrollers; title bar hidden
ViewController - a container view controller because I need the option to place some controls beneath the UITableView, sometimes (but not in the current scenario)
UITableViewController
Now, my question is what the proper frame dimensions of the UITableview should be. Here's what I've got in the ViewController viewDidLoad method. I used subtracted 49.0 (the size of the tab bar) from 480.0. However, this leaves a black bar at the bottom. 20.0 appears to do it (coincidentally?) the size of the status bar, but I don't understand why that would be. Wouldn't the true pixel dimensions of the tableview be 480-49?
// MessageTableViewController is my subclass of UITableViewController
MessagesTableViewController *vcMessagesTable = [[MessagesTableViewController alloc] init];
CGRect tableViewFrame = CGRectMake(0, 0, 320.0, 480.0 - 49.0);
[[vcMessagesTable view] setFrame:tableViewFrame];
self.tableViewController = vcMessagesTable;
[self addChildViewController:vcMessagesTable];
[[self view] addSubview:vcMessagesTable.view];
Here's how it looks:
I've run into this problem also -- I think it would be best not to hard code the size but to refer to the size of one its ancestor controllers. In this case, the UINavigationController, that's the direct child of the tabBar controller should have the right frame to fill the whole screen minus the tabBar. So I would subtract (if you need to) from that frame height if you want space at the bottom, otherwise, just use that frame. I think that self.navigationController finds the nearest nav controller above your controller of interest.

How can I add a transparent view?

I want to push a view controller onto the navigation stack but I don't want its view to initially appear - in other words I want the view that was visible when the view controller is push to still be visible.
I tried setting the view controller's view's alpha value to 0.0 which I thought would make it transparent. But instead what is happening is that when I push the view controller on the the stack the screen is white. If I set the alpha to 1.0 then the view controller's view appears as expected.
Why is it white and not transparent?
you will have to add the view to the viewcontrollers manually
Not pushing it
For example do the following
YourViewController *vc = [[YourViewController alloc] init];
[self.view addSubview:vc.view];
vc.view.alpha = 0.0;
//Animate Here
vc.view.alpha = 1.0;
//Commit Animate Here
Please not that you will have to do some additional coding to implement the release of the vc, since now you have retained vc.view you will not be able to release vc easily,
Another solution is instead of implementing the second view as a viewcontoller implement it as uiview, and the xib class will be view and not uiviewcontroller
Maybe make sure that the opaque property is set to NO?
Or perhaps the view you're pushing on was built in interface builder, and you have a background color of white with another view you put on top of it and you only changed the opacity of the subview?

UINavigationController has extra status bar gap at top

This looked simple enough when I set it up, but I can't explain why this gap is present between the status bar and the navigation bar. Also, the contained view looks like it may be properly aligned, and it's just the nav bar that is shifted down. The gap looks like the size of the status bar, so I expect that has something to do with it, but I don't know what.
Here is the code for setting up the navigation controller:
- (void)viewDidLoad
{
[super viewDidLoad];
advancedVC = [[AdvancedSearchFormVC alloc] initWithNibName:#"AdvancedSearchForm" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:advancedVC];
nav.navigationBar.tintColor = [UIColor defaultNavBarTint];
nav.navigationBar.topItem.title = NSLocalizedString(#"SearchTitle", nil);
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"SearchButton", nil) style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
nav.navigationBar.topItem.rightBarButtonItem = searchButton;
self.view = nav.view;
}
The rootViewController uses a view from a xib file, where I have simulated the status bar, the navigation bar, and the tab bar.
The problem is indeed that the navigation controller always expects to leave room for the status bar, which is the 20 pixel gap. I searched around for a while before I found this solution which works:
//nav is assumed to be a subclass or instance of UINavigationController
nav.view.frame = CGRectOffset(nav.view.frame, 0.0, -20.0);
//you can then add the navigation's view as a subview to something else
I originally found an answer which did this offset to the navigationbar's view, but it didn't work. It works when you do it to the navigation controllers actual view.
I use this technique to add a navigation controller from another nib to an empty view of my main nib, so I can position it anywhere within the main screen as a subview. By using an empty view as a placeholder and positioning frame on my main nib, I create a separate nib and class to manage the navigation, which manages other nibs used to handle their screens. This way I can solve the classic "how do I add a banner, image, or custom views above my navigation controller" while having a navigation controller as a subview...in iOS 5 to be specific.
It's also worth mentioning that I use the app delegate to store and access all the other controllers, so they are retained by a persistant instance which I can access from any class. Create and synthesise some properties in the app delegate of all your controllers, and in viewDidLoad create instances. That way I can reference all the controllers used in my app later anywhere by adding:
//this shows how to store your navigation controllers in the app delegate
//assumes you've added 2 properties (UINavigationController*)"navController" and (UIViewController*)"rootController" in your app delegate
//...don't forget to add #import "AppDelegate.h" to the top of the file
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app.navController pushViewController: app.rootController animated:YES];
//now apply the offset trick to remove the status gap
app.navController.view.frame = CGRectOffset(app.navController.view.frame, 0.0, -20.0);
I had the same problem before. The code I used to add UINavigationBar to UIViewController:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self];
[self.view addSubview:nc.view];
Solution:
Check the box "Wants Full Screen" with Attributes inspector of your UIViewController.
You can try to set the attribute Under Top Bars unchecked from Attributes section of UIViewController.
As we all know by now, the 20 pixel shift is to provide space for the Status bar on the top.
But infact, the view controllers coordinate system is kept in place and only the navigation bar frame is shifted down by 20 pixels. This makes the navigation bar to actually overlap the top 20 pixels of the view.
Logging the navigation bars frame origin, it will show (0.0, 20.0)
So the solution is to simply reposition the navigation bar's origin to (0.0, 0.0) in ViewWillAppear.
self.navigationController.navigationBar.frame = CGRectMake(0.0, 0.0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
Since you're adding advancedVC as a subview of self.view, it is being added inside the frame of self.view which I'm guessing is already compensating for the status bar.
You can probably easily fix this by adding this line:
nav.view.frame = self.view.frame;
Just above this line:
self.view = nav.view;
-
Other Thoughts
I'm not privy to your entire setup, but self.view may not be needed at all. Simply make your advancedVC instance the rootViewController of the UIWindow instance contained in your App Delegate.
The problem was resolved by fixing the way the navigation controller was inserted. Instead of inserting it into a view that had been put onto the tabbar controller, the navigaiton controller should have been put directly onto the navigation controller.
advancedSearchFormVC = [[AdvancedSearchFormVC alloc] initWithNibName:#"AdvancedSearchForm" bundle:nil];
UINavigationController *searchNavController = [[UINavigationController alloc] initWithRootViewController:advancedSearchFormVC];
This is just one controller that is on the tabbar controller, replacing the advancedSearchFormVC at the same time. Then this nav controller was added to the array of controllers that got put onto the tabbar controller.
Sorry for any trouble, but this was one of those problems I can look directly at and not see it. I should have seen this earlier, because I had another nav controller already on the tabbar controller, and it was set up the same way.
Thanks for your assistance.
The problem is that UINavigationController.view should be added to the top view.
Just find the top one and it will be working great.

Resources