Hiding UINavigationBar and Status Bar issue - ios

I'm trying to hide my NavigationBar and the Status Bar (slide up animation) and I'm running into an issue.
When the status bar is visible, the origin point of every element that is at 0 point (x: 0) means right underneath the statusbar. However, when the statusbar is hidden, the 0 (x: 0) point updates to accomodate the new space, and 0 (x: 0) means the absolute top of the screen.
When I hide the status bar and rotate into landscape, the view autosizes and everything is shifted to use the status bar's space, and throws off my animation:
if (![[UIApplication sharedApplication] isStatusBarHidden]) {
// Change to fullscreen mode
// Hide status bar and navigation bar
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:animationDuration animations:^{
navBar.frame = CGRectMake(navBar.frame.origin.x,
-navBar.frame.size.height-20,
navBar.frame.size.width,
navBar.frame.size.height);
} completion:^(BOOL finished) {
[navBar setHidden:TRUE];
}];
} else {
// Change to regular mode
// Show status bar and navigation bar
[navBar setHidden:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:animationDuration animations:^{
navBar.frame = CGRectMake(navBar.frame.origin.x,
0,
navBar.frame.size.width,
navBar.frame.size.height);
} completion:^(BOOL finished) {
}];
}
Any suggestions?
EDIT: Here's what the screen looks like after rotation's relayout: Image

You're confusing the subdivision of the screen into UIView areas a little bit.
When you're in a navigation controller, there are three views:
Navigation controller's "root" view
Inside that, the NavigationBar (which of course is a UIView
Also inside the navigation controller, a "content area" view
So the Nav controller is managing it's own root view. In that it is filling the space with a NavigationBar at the top, and the rest of the area with one big UIView for content.
When you push your view controller onto the navigation stack, the Nav controller is adding your root view as the content. So your entire "self.view" is completely contained within the Nav controller's "content" view.
And so, of course, when the Nav controller hides the navigation bar... the "content" view expands up to fill the space. And then that view tells your view "hey, there's more space than you're using so your view also expands up to completely fill the Nav controller's content view.
So your view's "0" point is always the top of your view. That never changes. What is changing is where "the top of your view" is relative to the top edge of the screen.
If you want your content to remain in same spot on screen when navbar is removed, then you're going to have to account for the fact that your "zero" point is now higher than it was when there was a nav bar pushing the content view down.

Related

Show View on the top of navigation bar

I have a custom navigation and Home view in my app. On the click of button I want to show a View just like a left side view ( width is not equal to screen width) , on the top of navigation.
I have tried this:
LeftView.Layer.ZPosition = 1;
It is not working. If I set the Z index of navigation to -1, this hides complete navigation bar.
Try this :
[self.navigationController.view addSubview:yourSubView];
Try to add that view to UIWindow. This will add your view above navigation bar.
[[[[UIApplication sharedApplication] delegate] window] addSubview:yourView];
Add your custom navigation bar to the main view manually instead of using navigation controller's standard navigation bar. Then add your leftview as subview, in your main view. It will overlap navigation bar in this hierarchy.
This is a correct answer:
[self.navigationController.view addSubview:yourSubView];
Though it will not set the correct frame for the view, To set the correct frame for your view on the navigation bar, you have to set it in the viewDidAppear.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.navigationController.view addSubview:mTitleView];
}

Adding a subview above the status bar iOS 7

Ive got a small rectangular view that animates down from the top of my app. I need it to be at the very top of the screen and animate down over the status bar, however the animated view is appearing under the status bar. Anyone know how I can get it over the status bar??
here is what Im doing currently
[self.navigationController.view addSubview:self.headerView];
It works perfectly and is in the correct position EXCEPT for the fact its underneath the status bar. Any ideas?
edit: I know this is possible because snapchat does it.
One option you can do is, when you animating the headerView of yours,
take snapshot of the statusbar
hide statusbar
add subview of snapshot view on the statusbar position
do the header view animation over snapshot view
remove snapshot view and show statusbar again.
This way you can get nice animation which looks like it's doing over status bar.
Edit: I will try to explain in pseudo-code
// 1. take snapshot of the status bar
UIView* snapshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
// 2. hide statusbar
_statusBarHidden = YES;
[self setNeedsStatusBarAppearanceUpdate];
// you need to set "View controller-based status bar appearance" option to yes on plist
- (BOOL)prefersStatusBarHidden
{
return _statusBarHidden;
}
// 3. add subview of snapshot view on the statusbar position
CGRect frame = self.view.frame;
frame.height = [[UIApplication sharedApplication] statusBarFrame].size.height;
[self.view addSubView:snapshotView];
// 4. do the header view animation over snapshot view
.. Just do the animation you already were doing
// 5. remove snapshot view and show statusbar again.
[snapshotView removeFromSuperview];
_statusBarHidden = NO;
[self setNeedsStatusBarAppearanceUpdate];

iOS MapView goes under navigation bar, status bar, and tab bar controller

I have a ViewController with a navigation controller and a tab bar controller. This ViewController has 2 buttons that toggle the visibility of a scroll view and a map view. The main thing is to have these 2 buttons always show up in the same place regardless of orientation or the view that happens to be visible.
The problem I am having is that the MapView won't size properly. If I just give it a frame from self.view.bounds it goes under the navigation bar / tab bar - basically taking up the whole screen. This throws off the location of my toggle buttons.
I noticed my ScrollView does the same (using a background color and a translucent navigation bar) but the positioning of sub views on it stay within the visible area (between the navigation bar and the tab bar). So when I add my toggle buttons, they show in the correct place.
When I press the toggle buttons, I just re-assign the parent view of the buttons to the now displayed view (ScrollView or MapView). This always works on the scroll view but due to the positioning, they end up going under the navigation bar when the MapView is displayed.
I have tried creating the frame for the MapView manually but I get odd results. I use this for the frame:
CGRect mapFrame = CGRectMake(
0,
(self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height),
self.view.frame.size.width,
(
self.view.frame.size.height
- (self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height + self.tabBarController.tabBar.frame.size.height)
)
);
I then set the auto resizing masks
[self.mapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
But with this, if I enter into the view controller in landscape mode - the MapView is off the screen. If I enter in portrait mode and then rotate to landscape, the top margin is off but about 10 points so it still goes under the navigation bar a bit (and has a visible margin between the tab bar and the bottom of the map view).
How can I make the MapView subviews only display within the visible region like the ScrollView does? I don't mind so much if the map itself goes under the navigation bar / tab bar
Ugh, I found the answer just a few minutes ago. I have no idea why this is the default behavior on iOS 7 but alas, there it is.
The solution is to add this to the viewDidLoad on the ViewController
[self setEdgesForExtendedLayout:UIRectEdgeNone];
[self setAutomaticallyAdjustsScrollViewInsets:NO];
Much thanks to the post here

How do I draw main view underneath my UINavigationBar so when the bar shows/hides, the view is unaffected?

Here's the situation:
I am making an app for iPad w/ iOS 6 using Autolayout along with UINavigationController. What I am trying to do is:
Segue from one view controller to the next with a standard push segue.
When I arrive at the new view controller, hide the nav bar with animation.
As the nav bar hides, I want my view to not shift at all. In fact, I want my view to effectively be drawn underneath the nav bar from the beginning, so I'm left with no shifting or movement of content and no black bars. For reference, this is what happens in the Amazon Kindle app when you go into a book.
With my current code, the contents of my view shift up to fill in the void left by the UINavigationBar.
I've tried force-setting the frame of my UIViewController's view and my UINavigationController's view to the entire iPad screen in the viewWillAppear method of my viewcontroller but no dice. I've experimented w/ Constraints in Autolayout but that also didn't get me to where I wanted to go.
Any help you can give would be great!
Try following before animating the navigation bar:
self.navigationController.navigationBar.alpha = 0.99f;
I didn't try this but this should work.
Looks like you need to add custom navigation bar in your new view and animate it to disappear.
I think, hiding original Navigation bar of Navigation Controller without shifting the view is not possible.
Rather add UINavigationBar to xib file, bind it to IBOutlet uiNavigationBar and try following code
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect f = self.uiNavigationBar.frame;
f.origin = CGPointMake(f.origin.x, f.origin.y - 44);
self.uiNavigationBar.frame = f;
} completion:^(BOOL finished) {
NSLog(#"done");
}];
}

Is it correct to move the navigation bar frame?

I have a navigation bar based ipad app.
At some point I want to push another view controller into the views controller hierarchy. Then, when the users tabs some button I want to show a leftMenu controller. To do so I have two views:
A content view which has all the content
And a not visible view which is the leftMenu. This one is under the content view.
So when the user presses the button, what Im doing right now is moving the content view and the navigation bar to the right to make the leftMenu visible:
self.navigationController.navigationBar.frame = CGRectMake(271.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.self.navigationController.navigationBar.frame.size.height);
self.contentView.frame = CGRectMake(271.0, self.contentView.frame.origin.y, self.contentView.frame.size.width, self.contentView.frame.size.height);
This is working, but the first row in the left menu is not "clickable" where the nav bar is supossed to be. Its like the navigation bar is still there capturing the tab events.
Is it correct to do?:
self.navigationController.navigationBar.frame = CGRectMake(271.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.self.navigationController.navigationBar.frame.size.height);
If not, whats the propper way to achieve what I want?
Heres and image ilustrating what the problem is:
I think it's best to use a custom container controller to do this kind of thing, rather than moving a navigation bar. In IB, this can be set up quite easily. Start with a UIViewController, add a container view to it, and size how you want. Then in the inspector, set its x value to minus its width, which will put it off screen to the left. Then add another container view and size it to be full screen. You can then delete the view controller that you got with that container view, and right drag from the container view to your initial navigation controller (of your already setup UI) to connect it up with an embed segue. The UIViewController that you started with should be made the initial view controller of the storyboard. To move in the side view, I use this code in that custom container controller:
-(void)slideInLeft {
if (isRevealed == NO) {
[UIView animateWithDuration:.6 animations:^{
leftView.center = CGPointMake(leftView.center.x + 100, leftView.center.y);
mainView.center = CGPointMake(mainView.center.x + 100, mainView.center.y);
} completion:^(BOOL finished) {
isRevealed = YES; ;
}];
}else{
[UIView animateWithDuration:.6 animations:^{
leftView.center = CGPointMake(leftView.center.x - 100, leftView.center.y);
mainView.center = CGPointMake(mainView.center.x - 100, mainView.center.y);
} completion:^(BOOL finished) {
isRevealed = NO;
}];
}
}
leftView and mainView are IBOutlets to the 2 container views. I call this method from a button in the main view controller (the root view controller of the navigation controller that's embedded in the large container view):
-(IBAction)callSlideIn:(id)sender {
[(ViewController *)self.navigationController.parentViewController slideInLeft];
}
I found a "fast" way to achieve this (and a bit hacky imo)
I added the leftMenu view to the top view in the views hierachy:
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:self.leftMenu.view];
Now it is les deep than the navigation bar and, of course, its clickable

Resources