UIView animation, condition of when view reached point x - ios

In my current animation, I'm moving an UIView from the bottom to the top. The view is added to the navigation controller's view so it will show on top of the navigation bar.
A simplified version of the animation:
UIView.animateWithDuration(0.5) { self.bar.frame.origin.y = 0 }
In this animation, the view will eventually reach the navigation bar. Is it possible to somehow get this condition and do something with the navigation bar?

Related

How to hide UINavigationBar and UITabBar animated?

I have a UIViewController with UINavigationBar and UITabBar. I want to hide them animated: UINavigationBar flip upward and UITabBar flip downward. During the animation, I need the UIViewController's view resizing automatically to fulfil the new frame.
How to do it?
My structure is: UIViewController is placed in a UINavigationController as rootViewController, and the UINavigationController is placed in a UITabBarController as a childViewController.
I have tried resize the AppDelegate.window.frame directly but it will make other views.frame incorrect.
Since iOS8 you can use:
self.navigationController.hidesBarsOnSwipe = YES;
INFO
There is a bunch of nice new methods since iOS8:
UINavigationController
hidesBarsOnTap: A Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.
hidesBarsOnSwipe: A Boolean value indicating whether the navigation bar hides its bars in response to a swipe gesture.
hidesBarsWhenVerticallyCompact: A Boolean value indicating whether the navigation controller hides its bars in a vertically compact environment.
hidesBarsWhenKeyboardAppears: A Boolean value indicating whether the navigation controller hides its bars when the keyboard appears.

Set image view underneath the navigation bar

In my UIViewController I have UIImageView that takes up the full size of the controller (screen) and serves as a background. On top of UIImageView I have a UITableView with a clear background. I set the navigation bar to translucent like this:
self.navigationController.navigationBar.translucent = YES;
UIImageView is underneath the navigation bar as I want it to be. Unfortunately the same happens to UITableView. I want to put UITableView at the bottom of navigation bar, leaving UIImageView underneath. If I set:
[self setEdgesForExtendedLayout:UIRectEdgeBottom]
then UITableView is at the bottom of the navigation bar, but the same happens also to UIImageView. What would be the easiest solution to leave UIImageView underneath the navigation bar and push down the UITableView at the bottom of the navigation bar?
By default in iOS 7 the content extends to the top and bottom of the screen, underneath any navigation bars, tool bars, or tab bars. If you were to set the frame of the table view to be start after the navigation bar, the content of the table view would not scroll beneath the navigation bar providing the nice blur effect.
What you'll probably want to do instead, is keep the y origin of your table view at 0, underneath the navigation bar, and set the content inset so the content starts after the navigation bar.
This is pretty simple. If you're using auto layout the top layout guide of the view controller will recognise the height of the status bar and navigation bar so you don't need to calculate this yourself. The bottom of the navigation bar should end at 66.
//Using Auto Layout
CGFloat navigationBarHeight = self.topLayoutGuide.length;
//Not using Auto Layout
UINavigationBar *nav = self.navigationController.navigationBar;
CGFloat navigationBarHeight = nav.frame.origin.y + nav.frame.size.height;
myTableView.contentInset = UIEdgeInsetsMake(navigationBarHeight, 0, 0, 0)
And of course if you actually do want the frame to start after the navigation bar the height above stays the same, you just need to manually set the frame of the table view.
CGRect tableViewFrame = self.view.bounds;
tableViewFrame.origin = CGPointMake(tableViewFrame.origin.x, navigationBarHeight);
tableViewFrame.size = CGSizeMake(tableViewFrame.size.width, tableViewFrame.size.height - navigationBarHeight*2);
myTableView.frame = tableViewFrame;
EDIT: Ah, almost forgot. You'll also want to change the scrollIndicatorInsets to match the contentInset, so your scroll bars don't move offscreen.
myTableView.scrollIndicatorInsets = myTableView.contentInset;

iOS - pop a view controller by panning on the left edge, navigation bar disappears

So iOS 7 introduced this new feature that you can pop a view controller by panning on the left edge. Here is my problem: I have two view controllers, A and B, that are connected by a push segue. Both of the controllers have navigation bars (by embedding A in a navigation controller). The navigation bar in B will be hidden once the user enters B's scene, and can be shown if the user taps on the scene. If the user pans on the left edge of B while the navigation bar is hidden, the navigation bar in A will be hidden as well, which means that there is no way for the user to return further back from A. So is there a way to enforce A to always show the navigation bar regardless of B has hidden the bar or not? Or is there a easy way to prevent the pan gesture from taking effect? I read this post which suggested a way of preventing the pan, but I can't locate the property in storyboard.
EDIT: So I disabled the interactive pop gesture recognizer but that only solved half of the problem. The other half is that if I click the back button on the child view controller navigation bar when the navigation bar is disappearing, I am navigated back to the parent view controller without a navigation bar. I tried calling [self.navigationController setNavigationBarHidden:NO] in viewWillAppear and then viewDidLoad but it does not work. Is this some sort of bug in the SDK or am I missing something?
Here is the code for hiding the navigation bar in the child view controller
- (void)hideNavigationBar
{
if (self.navigationBarHidden == NO)
{
[UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
self.navigationController.navigationBar.alpha = 0.0;
self.previewCollectionView.alpha = 0.0;
} completion:^(BOOL finished) {
self.navigationBarHidden = YES;
}];
}
}
Yes, you can enforce the navigation bar's appearance in the A viewController's -viewWillAppear method.
Also, since you cannot find the interactivePopGestureRecognizer property in the storyboard, you can use this line in the A viewController's -viewDidLoad method:
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
EDIT:
In the viewWillAppear method, you will have to call:
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.alpha = 1.0;
I see a couple problems with your situation:
You disable the interactive pop gesture and you hide the nav bar from view controller B. How is the user supposed to intuitively go back?
The animation that hides your navbar in B may be causing the issue. If it's anything longer than a split second, that animation may not complete in time before you hit the back button and -viewWillAppear fires on A.
Your code in B hides the navigation bar for the navigation controller. The navigation controller that holds view controller A is the same instance that holds view controller B. If you hide the navigation bar when B loads, then you go back to A (not sure how you're doing that without a back button or a edge pan gesture), it should still be hidden.
You probably want NOT disable the gesture (so the user can intuitively go back) and turn the navigation bar back on in view controller A's -viewWillAppear, to cover the case where you turned it off in B:
if (self.navigationBarHidden == NO)
{
self.navigationController.navigationBar.alpha = 1.0;
self.previewCollectionView.alpha = 1.0;
self.navigationBarHidden = NO;
}

Place element right under navigation bar on scroll

If I have an element in a scroll view(not positioned in the top) how would I be able to let this element stay right under the navigation bar when it hits the bar on scroll ? Like the shuffle bar in the spotify app ?
In your ScrollView delegate:
Implement viewDidScroll:
Check the offset of the scroll view in this method: adjust the position of your nav bar using the negative offset of the scrollview: self.navBar.frame.origin.y = - view.yOffset;

Image view hide navigation bar in scroll view

In my ipad app, there is one navigation bar and one image view within scroll view.
When i run my app, the image view hide the navigation bar.
So, i remove image view and run again, then i see navigation bar.
But, i can't click the button in navigation bar.
Please, tell me how can i solve it?
Keep in mined the hierarchy of the view , if the UIScrollView is on top of the views , then any view behinde it will not receive any touch event.
The navigation bar added with XIB is a part of the subviews in you main view so you have to start the UIScrollView under the navigation bar , y = 44
ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width,self.view.frame.size.height-44)];
and do not forget to subtract 44 from the ScrollView height to fit the screen.

Resources